From 0f6fb60746f003f3603c9bd231303d35f768e5aa Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Wed, 22 Mar 2023 17:18:19 +0100 Subject: [PATCH] email seting in profile --- back/src/auth/auth.controller.ts | 13 ------------ back/src/chat/dto/create-channel.dto.ts | 2 ++ back/src/users/dto/user.dto.ts | 8 +++++-- back/src/users/users.controller.ts | 4 ++-- front/src/Auth.ts | 23 ++------------------ front/src/components/Channels.svelte | 4 ---- front/src/components/Profile.svelte | 28 ++++++++++++++----------- 7 files changed, 28 insertions(+), 54 deletions(-) diff --git a/back/src/auth/auth.controller.ts b/back/src/auth/auth.controller.ts index e874f2a..a0ea023 100644 --- a/back/src/auth/auth.controller.ts +++ b/back/src/auth/auth.controller.ts @@ -36,19 +36,6 @@ export class AuthController { private readonly usersService: UsersService ) {} - @Post('/email') - @UseGuards(AuthenticatedGuard) - async setEmail ( - @Profile42() profile: Profile, - @Body() body: EmailDto - ): Promise { - const email = body.email - const user = (await this.usersService.getFullUser(+profile.id)) - user.email = email - console.log(`email sent to ${user.email}`) - await this.usersService.save(user) - } - @Get('in') @UseGuards(FtOauthGuard) ftAuth (): void {} diff --git a/back/src/chat/dto/create-channel.dto.ts b/back/src/chat/dto/create-channel.dto.ts index f71f5ff..c3f8973 100644 --- a/back/src/chat/dto/create-channel.dto.ts +++ b/back/src/chat/dto/create-channel.dto.ts @@ -22,8 +22,10 @@ export class CreateChannelDto { isPrivate: boolean @IsBoolean() + @IsOptional() isDM: boolean @IsString() + @IsOptional() otherDMedUsername: string } diff --git a/back/src/users/dto/user.dto.ts b/back/src/users/dto/user.dto.ts index fe60681..26962f9 100644 --- a/back/src/users/dto/user.dto.ts +++ b/back/src/users/dto/user.dto.ts @@ -1,4 +1,4 @@ -import { IsString, IsNotEmpty, IsPositive, IsOptional } from 'class-validator' +import { IsString, IsNotEmpty, IsPositive, IsOptional, IsEmail, NotContains } from 'class-validator' import { ApiProperty } from '@nestjs/swagger' import { Express } from 'express' @@ -8,10 +8,14 @@ export class UserDto { @IsOptional() readonly ftId: number - @IsString() @IsNotEmpty() + @NotContains(' ') readonly username: string + @IsEmail() + @IsNotEmpty() + readonly email: string + @IsOptional() readonly status: string diff --git a/back/src/users/users.controller.ts b/back/src/users/users.controller.ts index 86c9e39..22ef818 100644 --- a/back/src/users/users.controller.ts +++ b/back/src/users/users.controller.ts @@ -200,7 +200,7 @@ export class UsersController { @Get() @UseGuards(AuthenticatedGuard) async getUser (@Profile42() profile: Profile): Promise { - return await this.usersService.findUser(profile.id) + return await this.usersService.findUser(+profile.id) } @Post() @@ -209,7 +209,7 @@ export class UsersController { @Body() payload: UserDto, @Profile42() profile: Profile ): Promise { - const user = await this.usersService.findUser(profile.id) + const user = await this.usersService.findUser(+profile.id) if (user == null) throw new BadRequestException('User not found.') await this.usersService.update(user, payload) return user diff --git a/front/src/Auth.ts b/front/src/Auth.ts index 3081f5e..32d2a4d 100644 --- a/front/src/Auth.ts +++ b/front/src/Auth.ts @@ -32,32 +32,13 @@ export function login() { } export async function verify() { - if (get(store).isVerified === true) return; - let email : string; - await show_popup("Enter your preferred email adress:\n(defaults to 42 email)") - email = get(content); - if (email !== undefined && email !== '' && email !== 'ok') { - const body: EmailDto = { email } - const response = await fetch(API_URL + "/log/email", { - method: "POST", - mode: "cors", - headers: {"Content-Type": "application/json",}, - credentials: "include", - body: JSON.stringify(body) - }) - if (response.ok) {await show_popup("Email set",false)} - else {await show_popup("Couldn't set Email",false); return } - } const response = await fetch(API_URL + "/log/verify", { method: "GET", mode: "cors", credentials: "include", }); - if (response.ok) { - await show_popup("We have sent you an email to verify your account. Check your mailbox!.", false); - } else { - await show_popup("Email doensn't seem valid", false); - } + if (response.ok) + await show_popup(`We have sent you an email to verify your account. email: ${get(store).email} If you can't acces this mailbox, you still can contact us at vaganiwast@gmail.com to start unlocking your account.`, false); } export function logout() { diff --git a/front/src/components/Channels.svelte b/front/src/components/Channels.svelte index 4616caf..c997d6d 100644 --- a/front/src/components/Channels.svelte +++ b/front/src/components/Channels.svelte @@ -110,10 +110,6 @@ await show_popup("Channel name cannot contain #", false) return; } - if (channels.some((chan) => chan.name === name)) { - await show_popup("A channel with this name already exist", false) - return; - } if (name) { if (channelMode === 'protected'){ await show_popup("Enter a password for the new channel:", true, true) diff --git a/front/src/components/Profile.svelte b/front/src/components/Profile.svelte index 77deb1a..8029d30 100644 --- a/front/src/components/Profile.svelte +++ b/front/src/components/Profile.svelte @@ -20,12 +20,12 @@ import type { UserDto } from "./dtos/user.dto"; export let username: string = $store.username; + export let email: string = $store.email; export let gamePlaying: boolean; export let resetGameConnection: () => void = () => {}; let edit: boolean = true; let user: Player = $store; - let newUsername: string = $store.username; let avatarForm: HTMLFormElement; @@ -44,15 +44,12 @@ }); const dispatch = createEventDispatcher(); + async function handleSubmit() { if (gamePlaying) { popup.set(bind(Alert, { message: "Cannot change username while playing.", form: false })) return; } - if ($store.username === newUsername) { - popup.set(bind(Alert, { message: `Username is already set to ${newUsername}.`, form: false })) - return; - } const body: UserDto = { username: newUsername, @@ -66,14 +63,20 @@ const response = await fetch(API_URL + "/users", { headers: { "content-type": "application/json" }, method: "POST", - body: JSON.stringify({ username: newUsername }), + body: JSON.stringify({ + username: username, + email: email + }), credentials: "include", }); if (response.ok) { - $store.username = newUsername; - username = newUsername; - popup.set(bind(Alert, { message: "Succefully changed username.", form: false })) + $store.username = username; + $store.email = email; + popup.set(bind(Alert, { message: "Succefully changed informations.", form: false })) resetGameConnection(); + } else { + const error = await response.json(); + popup.set(bind(Alert, { message: error.message, form: false})) } } @@ -138,12 +141,13 @@ {#if edit}
- -