diff --git a/back/volume/package-lock.json b/back/volume/package-lock.json index 9ff4551..0a84acd 100644 --- a/back/volume/package-lock.json +++ b/back/volume/package-lock.json @@ -44,7 +44,7 @@ "passport-42": "^1.2.6", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", - "rxjs": "^7.2.0", + "rxjs": "^7.8.0", "socket.io": "^4.6.1", "source-map-support": "^0.5.21", "typeorm": "^0.3.12" diff --git a/back/volume/package.json b/back/volume/package.json index 4c539bb..753832b 100644 --- a/back/volume/package.json +++ b/back/volume/package.json @@ -56,7 +56,7 @@ "passport-42": "^1.2.6", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", - "rxjs": "^7.2.0", + "rxjs": "^7.8.0", "socket.io": "^4.6.1", "source-map-support": "^0.5.21", "typeorm": "^0.3.12" diff --git a/back/volume/src/auth/auth.controller.ts b/back/volume/src/auth/auth.controller.ts index 0033445..5103aba 100644 --- a/back/volume/src/auth/auth.controller.ts +++ b/back/volume/src/auth/auth.controller.ts @@ -18,7 +18,7 @@ import { Profile42 } from './42.decorator' import { AuthService } from './auth.service' import { UsersService } from 'src/users/users.service' import { EmailDto } from 'src/chat/dto/updateUser.dto' -import User from 'src/users/entity/user.entity' +import type User from 'src/users/entity/user.entity' const frontHost = process.env.HOST !== undefined && process.env.HOST !== '' @@ -30,7 +30,7 @@ const frontPort = : '80' @Controller('log') -export class AuthController { +export class AuthController { constructor ( private readonly authService: AuthService, private readonly usersService: UsersService @@ -42,10 +42,10 @@ export class AuthController { @Profile42() profile: Profile, @Body() body: EmailDto ): Promise { - console.log('in') const email = body.email - const user = (await this.usersService.findUser(+profile.id)) as User + const user = (await this.usersService.getFullUser(+profile.id)) user.email = email + console.log(`email sent to ${user.email}`) await this.usersService.save(user) } diff --git a/back/volume/src/auth/auth.service.ts b/back/volume/src/auth/auth.service.ts index 0db7907..6c3a64b 100644 --- a/back/volume/src/auth/auth.service.ts +++ b/back/volume/src/auth/auth.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common' +import { BadRequestException, Injectable } from '@nestjs/common' import { type User } from 'src/users/entity/user.entity' import { UsersService } from 'src/users/users.service' import { MailerService } from '@nestjs-modules/mailer' @@ -25,16 +25,22 @@ export class AuthService { async sendConfirmationEmail (user: User): Promise { user.authToken = Math.floor(10000 + Math.random() * 90000).toString() + console.log(`email sent to ${user.email}`) await this.usersService.save(user) - await this.mailerService.sendMail({ - to: user.email, - subject: 'Welcome to ft_transcendence! Confirm Email', - template: 'confirm', - context: { - username: user.username, - code: user.authToken - } - }) + try { + await this.mailerService.sendMail({ + to: user.email, + subject: 'Welcome to ft_transcendence! Confirm Email', + template: 'confirm', + context: { + username: user.username, + code: user.authToken + } + }) + } catch { + throw new BadRequestException("Email doesnt't seem to be valid") + } + console.log(`email sent to ${user.email}`) } async verifyAccount (code: string): Promise { diff --git a/back/volume/src/chat/chat.controller.ts b/back/volume/src/chat/chat.controller.ts index d7b1031..bac2c0f 100644 --- a/back/volume/src/chat/chat.controller.ts +++ b/back/volume/src/chat/chat.controller.ts @@ -21,6 +21,7 @@ import type User from 'src/users/entity/user.entity' import type Channel from './entity/channel.entity' import { Profile42 } from 'src/auth/42.decorator' import { Profile } from 'passport-42' +import { IsNumberString, IsPositive } from 'class-validator' @Controller('channels') @UseGuards(AuthenticatedGuard) @@ -140,7 +141,9 @@ export class ChatController { ): Promise { const channel = await this.channelService.getFullChannel(id) const user: User | null = await this.usersService.findUser(target.data[0]) - console.log(target) + if (isNaN(+target.data[1])) { + throw new BadRequestException(`Invalid duration ${target.data[1]}`) + } if (user == null) { throw new NotFoundException(`User #${target.data[0]} not found`) } @@ -167,6 +170,9 @@ export class ChatController { ): Promise { const channel = await this.channelService.getFullChannel(id) const user: User | null = await this.usersService.findUser(mute.data[0]) + if (isNaN(+mute.data[1])) { + throw new BadRequestException(`Invalid duration ${mute.data[1]}`) + } if (user == null) { throw new NotFoundException(`User #${mute.data[0]} not found`) } diff --git a/back/volume/src/chat/chat.gateway.ts b/back/volume/src/chat/chat.gateway.ts index 2c0c793..79062ae 100644 --- a/back/volume/src/chat/chat.gateway.ts +++ b/back/volume/src/chat/chat.gateway.ts @@ -138,8 +138,8 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { const user = (await this.userService.findUser(kick.to)) as User const connect = (await this.connectedUserRepository.findOneBy({ user: user.ftId - })) as ConnectedUser - if (connect) { + })) + if (connect !== null) { console.log(`kicking ${user.username} from ${channel.name} with socket ${connect.socket}`) this.server.to(connect.socket).emit('kicked') } diff --git a/back/volume/src/main.ts b/back/volume/src/main.ts index 2f9ab51..6f9dc7b 100644 --- a/back/volume/src/main.ts +++ b/back/volume/src/main.ts @@ -1,4 +1,4 @@ -import { InternalServerErrorException, Logger } from '@nestjs/common' +import { InternalServerErrorException, Logger, ValidationPipe } from '@nestjs/common' import { NestFactory } from '@nestjs/core' import { AppModule } from './app.module' import * as session from 'express-session' @@ -15,13 +15,17 @@ async function bootstrap (): Promise { ? +process.env.BACK_PORT : 3001 const cors = { - origin: new RegExp(`^(http|ws)://${process.env.HOST ?? 'localhost'}(:\\d+)?$`), + origin: new RegExp( + `^(http|ws)://${process.env.HOST ?? 'localhost'}(:\\d+)?$` + ), methods: 'GET, HEAD, PUT, PATCH, POST, DELETE, OPTIONS', preflightContinue: false, optionsSuccessStatus: 204, credentials: true, - allowedHeaders: ['Accept', 'Content-Type', 'Authorization'] + allowedHeaders: + ['Accept', 'Content-Type', 'Authorization'] } + app.useGlobalPipes(new ValidationPipe()) app.use( session({ resave: false, diff --git a/front/volume/src/Auth.ts b/front/volume/src/Auth.ts index 56d9c6e..8323321 100644 --- a/front/volume/src/Auth.ts +++ b/front/volume/src/Auth.ts @@ -31,6 +31,7 @@ export function login() { } export async function verify() { + if (get(store).twoFa === true) return; let email : string; await show_popup("Enter your preferred email adress:\n(defaults to 42 email)") email = get(content); @@ -46,7 +47,6 @@ export async function verify() { }) if (response.ok) {await show_popup("Email set",false)} else {await show_popup("Couldn't set Email",false); return } - console.log(response.ok) } console.log(API_URL) const response = await fetch(API_URL + "/log/verify", { @@ -56,9 +56,11 @@ export async function verify() { }); console.log(response.ok) if (response.ok) { - console.log("here") + console.log("here") 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);} + } else { + console.log("there") + await show_popup("Email doensn't seem valid", false);} } diff --git a/front/volume/src/components/Alert/Alert.svelte b/front/volume/src/components/Alert/Alert.svelte index 8006adf..8ceb35b 100644 --- a/front/volume/src/components/Alert/Alert.svelte +++ b/front/volume/src/components/Alert/Alert.svelte @@ -5,7 +5,7 @@ export let onOkay = () => {}; import { content, popup } from "./content"; - let value; + let value = ''; let onChange = () => { $content = ""; }; diff --git a/front/volume/src/components/Alert/content.ts b/front/volume/src/components/Alert/content.ts index 2813a6c..dfbf7d7 100644 --- a/front/volume/src/components/Alert/content.ts +++ b/front/volume/src/components/Alert/content.ts @@ -10,8 +10,7 @@ export async function show_popup(message, form = true) { message, form })) - await waitForCondition() - await waitForCondition() + await waitForCondition() } export async function waitForCondition() { @@ -25,5 +24,5 @@ export async function waitForCondition() { return await checkFlag(); } } - return checkFlag() + return await checkFlag() } diff --git a/front/volume/src/components/Chat.svelte b/front/volume/src/components/Chat.svelte index 58dcbad..101f45a 100644 --- a/front/volume/src/components/Chat.svelte +++ b/front/volume/src/components/Chat.svelte @@ -210,6 +210,8 @@ "Enter a time for which the user will be banned from this channel" ); const duration = $content; + if (duration == "") + return; response = await fetch(API_URL + "/channels/" + channel.id + "/ban", { credentials: "include", method: "POST", @@ -217,16 +219,15 @@ headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ data: target.ftId, duration }), + body: JSON.stringify({ data: [target.ftId, duration] }), }); if (response.ok) { await show_popup(`User banned for: ${duration} seconds`, false); socket.emit("kickUser", channel.id, $store.ftId, target.ftId); } else { - const error = await response.json(); - await show_popup(error.message, false); - } - socket.emit("kickUser", channel.id, $store.ftId, target.ftId); + const error = await response.json(); + await show_popup(error.message, false) + } } };