Browse Source

email seting in profile

master
nicolas-arnaud 2 years ago
parent
commit
0f6fb60746
  1. 13
      back/src/auth/auth.controller.ts
  2. 2
      back/src/chat/dto/create-channel.dto.ts
  3. 8
      back/src/users/dto/user.dto.ts
  4. 4
      back/src/users/users.controller.ts
  5. 23
      front/src/Auth.ts
  6. 4
      front/src/components/Channels.svelte
  7. 28
      front/src/components/Profile.svelte

13
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<void> {
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 {}

2
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
}

8
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

4
back/src/users/users.controller.ts

@ -200,7 +200,7 @@ export class UsersController {
@Get()
@UseGuards(AuthenticatedGuard)
async getUser (@Profile42() profile: Profile): Promise<User | null> {
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<User> {
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

23
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() {

4
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)

28
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 @@
</div>
{#if edit}
<form
id="username-form"
id="update-form"
class="username"
on:submit|preventDefault={handleSubmit}
>
<input type="text" id="username" bind:value={newUsername} required/>
<button type="submit" class="username" form="username-form"
<input type="text" id="username" bind:value={username} required/>
<input type="text" id="email" bind:value={email} required/>
<button type="submit" class="username" form="update-form"
>Change</button
>
</form>

Loading…
Cancel
Save