From 356e1caac364e6c639d41b70d5ed52f78b66b6fe Mon Sep 17 00:00:00 2001 From: simplonco Date: Sat, 14 Jan 2023 13:07:18 +0100 Subject: [PATCH] wip protected, already solved error http handling for createroom --- .../api_back/src/chat/chat.controller.ts | 54 +++++++++--- .../nestjs/api_back/src/chat/chat.service.ts | 20 ++--- .../src/pieces/chat/Interface_chat.ts | 11 +++ .../src/pieces/chat/Layout_create.svelte | 11 +-- .../src/pieces/chat/Layout_protected.svelte | 18 ++-- .../src/pieces/chat/Request_rooms.ts | 85 ++++++++++++------- .../src/pieces/chat/Request_utils.ts | 29 +++++++ 7 files changed, 163 insertions(+), 65 deletions(-) create mode 100644 srcs/requirements/svelte/api_front/src/pieces/chat/Request_utils.ts diff --git a/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts b/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts index 53551842..975276ff 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts @@ -111,8 +111,8 @@ export class ChatController { if (test_regex.test(room.name) === false) { let forbidden_chars = room.name.replace(new RegExp(regex_base, "g"), ""); - console.log(`throw error: Your room name can not contains these characters : ${forbidden_chars}`); - throw new HttpException( `Your room name can not contains these characters : ${forbidden_chars}`, HttpStatus.UNPROCESSABLE_ENTITY); + console.log(`throw error: display: true, code: 'FORBIDDEN_CHARACTERS', message: 'Your room name can not contains these characters : ${forbidden_chars}'`); + throw new HttpException({ display: true, code: 'FORBIDDEN_CHARACTERS', message: `Your room name can not contains these characters : ${forbidden_chars}` }, HttpStatus.OK); } if (typeof room.protection === 'undefined') @@ -121,8 +121,8 @@ export class ChatController { { if (!room.password || room.password.length === 0) { - console.log(`throw error: code: 'PASSWORD_BAD_FORMAT', message: 'your password is too short'`); - throw new HttpException({ code: 'PASSWORD_BAD_FORMAT', message: `your password is too short` }, HttpStatus.UNPROCESSABLE_ENTITY); + console.log(`throw error: display: true, code: 'PASSWORD_TOO_SHORT', message: 'your password is too short'`); + throw new HttpException({ display: true, code: 'PASSWORD_TOO_SHORT', message: `your password is too short` }, HttpStatus.OK); } } room.users = [req.user.username]; @@ -156,18 +156,18 @@ export class ChatController { const room_db = await this.chatService.getRoomByName(room.name, fields); if (room_db.type === 'direct') { - console.log("throw error: cannot join a direct messages room"); - throw new HttpException( `cannot join a direct messages room`, HttpStatus.CONFLICT); + console.log("throw error: display: true, code: 'JOIN_DIRECT_FORBIDDEN', message: 'cannot join a direct messages room'"); + throw new HttpException({ display: true, code: 'JOIN_DIRECT_FORBIDDEN', message: `cannot join a direct messages room` }, HttpStatus.OK); } if (room_db.type === 'private') { - console.log("throw error: cannot join a private room"); - throw new HttpException( `cannot join a private room`, HttpStatus.CONFLICT); + console.log("throw error: display: true, code: 'JOIN_PRIVATE_FORBIDDEN', message: 'cannot join a private room'"); + throw new HttpException({ display: true, code: 'JOIN_PRIVATE_FORBIDDEN', message: `cannot join a private room` }, HttpStatus.OK); } if (room_db.users.includes(req.user.username)) { - console.log("throw error: your have already joined this room"); - throw new HttpException( `your have already joined this room`, HttpStatus.CONFLICT); + console.log("throw error: display: true, code: 'ALREADY_JOIN', message: 'your have already joined this room'"); + throw new HttpException({ display: true, code: 'ALREADY_JOIN', message: `your have already joined this room` }, HttpStatus.OK); } room = await this.chatService.addUserToRoom(req.user.username, room.name); } @@ -188,14 +188,42 @@ export class ChatController { { console.log("- in changeRoom controller"); + let fields = ["protection", "allowed_users"]; + const room_db = await this.chatService.getRoomByName(room.name, fields); + if (room_db.protection === true) + { + if (!room_db.allowed_users.includes(req.user.username)) + { + console.log("throw error: display: true, code: 'NEED_AUTHENTICATE', message: 'You didn't provide the password for this room'"); + throw new HttpException({ display: true, code: 'NEED_AUTHENTICATE', message: `You didn't provide the password for this room` }, HttpStatus.OK); + } + } + + await this.chatService.setCurrentRoom(req.user.username, room.name); + let socket: socketDto = this.chatGateway.sockets.get(req.user.username); + await this.chatService.socketChangeRoom(socket, room.name); + + const ret_room = this.format_room(room); + res.status(HttpStatus.OK).json({ room: ret_room }); + + console.log("- out changeRoom controller"); + } + + @UseGuards(AuthenticateGuard) + @UseGuards(TwoFactorGuard) + @Post('password') + async setPassword(@Body() room: roomDto, @Req() req, @Res() res): Promise + { + console.log("- in setPassword controller"); + let fields = ["protection", "allowed_users"]; const room_db = await this.chatService.getRoomByName(room.name, fields); if (room_db.protection === true) { if (!room.password) { - console.log("throw error: code: 'PASSWORD_MISSING', message: 'this room is protected, you need to provide a password'"); - throw new HttpException({ code: 'PASSWORD_MISSING', message: `this room is protected, you need to provide a password` }, HttpStatus.BAD_REQUEST); + console.log("throw error: display: true, code: 'PASSWORD_MISSING', message: 'this room is protected, you need to provide a password'"); + throw new HttpException({ display: true, code: 'PASSWORD_MISSING', message: `this room is protected, you need to provide a password` }, HttpStatus.OK); } if (!room_db.allowed_users.includes(req.user.username)) await this.chatService.setPasswordValidation(req.user.username, room); @@ -208,7 +236,7 @@ export class ChatController { const ret_room = this.format_room(room); res.status(HttpStatus.OK).json({ room: ret_room }); - console.log("- out changeRoom controller"); + console.log("- out setPassword controller"); } @UseGuards(AuthenticateGuard) diff --git a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts index 8b3e30c3..b0618d62 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts @@ -194,8 +194,8 @@ export class ChatService { const is_match = await bcrypt.compare(room.password, room_db.hash); if (!is_match) { - console.log(`throw error: code: 'BAD_PASSWORD', message: 'bad password'`); - throw new HttpException({ code: 'BAD_PASSWORD', message: `bad password` }, HttpStatus.UNAUTHORIZED); + console.log(`throw error: display: true, code: 'BAD_PASSWORD', message: 'bad password'`); + throw new HttpException({ display: true, code: 'BAD_PASSWORD', message: `bad password` }, HttpStatus.OK); } room_db.allowed_users.push(username); @@ -215,8 +215,8 @@ export class ChatService { const find_room = await this.getRoomByName(room.name); if (find_room) { - console.log("throw error: This room name already exist"); - throw new HttpException( `This room name already exist`, HttpStatus.CONFLICT); + console.log("throw error: display: true, code: 'ROOM_CONFLICT', message: 'This room name already exist'"); + throw new HttpException({ display: true, code: 'ROOM_CONFLICT', message: `This room name already exist` }, HttpStatus.OK); } let hash; @@ -225,8 +225,8 @@ export class ChatService { console.log("in room protection hash"); if (room.type === 'direct') { - console.log("throw error: code: 'DIRECT_PASSWORD_FORBIDDEN', message: 'you cannot set a password in a direct message room'"); - throw new HttpException({ code: 'DIRECT_PASSWORD_FORBIDDEN', message: `you cannot set a password in a direct message room`}, HttpStatus.CONFLICT); + console.log("throw error: display: true, code: 'DIRECT_PASSWORD_FORBIDDEN', message: 'you cannot set a password in a direct message room'"); + throw new HttpException({ display: true, code: 'DIRECT_PASSWORD_FORBIDDEN', message: `you cannot set a password in a direct message room`}, HttpStatus.OK); } const saltOrRounds = 10; const password = room.password; @@ -295,13 +295,13 @@ export class ChatService { const room = await this.getRoomByName(room_name); if (!room.users.includes(username)) { - console.log("throw error: your are not in this room"); - throw new HttpException(`your are not in this room`, HttpStatus.CONFLICT); + console.log("throw error: display: true, code: 'USER_NOT_FOUND', message: 'your are not in this room'"); + throw new HttpException({ display: true, code: 'USER_NOT_FOUND', message: `your are not in this room` }, HttpStatus.OK); } if (room.type === "direct") { - console.log("throw error: code: 'LEAVING_DIRECT_FORBIDDEN', message: 'you cannot leave a direct messages conversation'"); - throw new HttpException({ code: `LEAVING_DIRECT_FORBIDDEN`, message: `you cannot leave a direct messages conversation`, status: HttpStatus.CONFLICT }, HttpStatus.CONFLICT); + console.log("throw error: display: true, code: 'LEAVE_DIRECY_FORBIDDEN', message: 'you cannot leave a direct messages conversation'"); + throw new HttpException({ display: true, code: 'LEAVE_DIRECY_FORBIDDEN', message: `you cannot leave a direct messages conversation` }, HttpStatus.OK); } // delete user from room diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Interface_chat.ts b/srcs/requirements/svelte/api_front/src/pieces/chat/Interface_chat.ts index ca9b761c..e634f732 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Interface_chat.ts +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Interface_chat.ts @@ -3,4 +3,15 @@ export interface Room name: string; type: "public" | "protected" | "private" | "direct" | "user"; users?: string[]; + client_name?: string; + protection?: boolean; +} + +export interface FetchResponse +{ + status: number; + code?: string; + display?: boolean; + message?: string; + room?: any; } diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_create.svelte b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_create.svelte index 59e4aeb2..b6f59530 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_create.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_create.svelte @@ -3,6 +3,7 @@ import { msgs, layout, allowed_chars } from './Store_chat'; import { change_room, create_room } from './Request_rooms'; import { onMount } from 'svelte'; + import type { FetchResponse } from './Interface_chat'; import Button from './Element_button.svelte'; import Warning from './Element_warning.svelte'; @@ -22,10 +23,8 @@ let room_type: string; let is_protected = false; let room_password: string; - let response = { - status: 0, - message: "", - } + let response: FetchResponse; + let show_error = false; async function handleSubmit(evt) { @@ -37,12 +36,14 @@ let room = { name: room_name, type: room_type, + protection: is_protected, }; if (is_protected === true) room.password = room_password; // send the new room response = await create_room(room); + show_error = response.display; // go to room if (response.status === 200) await change_room(response.room); @@ -70,7 +71,7 @@
- {#if response.status >= 300} + {#if show_error} {/if} diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_protected.svelte b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_protected.svelte index 0551f91d..284464e5 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_protected.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_protected.svelte @@ -1,17 +1,16 @@ @@ -59,7 +63,7 @@
- {#if response.status >= 300} + {#if show_error} {/if} diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Request_rooms.ts b/srcs/requirements/svelte/api_front/src/pieces/chat/Request_rooms.ts index 4a567579..02ffaafc 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Request_rooms.ts +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Request_rooms.ts @@ -1,5 +1,6 @@ import { msgs, user, layout, socket, current_room_name, current_room_type } from './Store_chat'; -import type { Room } from './Interface_chat'; +import type { Room, FetchResponse } from './Interface_chat'; +import { set_client_name_on_room, fill_fetch_response } from './Request_utils'; export async function get_room_messages() { @@ -24,23 +25,31 @@ export async function create_room(room: Room) { console.log("in create_room"); + let response: FetchResponse = { status: 0 }; + // send the new room - const response = await fetch('/api/v2/chat/create', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(room), - }); + try { + const resp = await fetch('/api/v2/chat/create', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(room), + }); + console.log("resp.status:"); + console.log(resp.status); + response.status = resp.status; + if (!resp.ok) + throw new Error(resp.statusText); - // get response status and message - let response_status = response.status; - let data = await response.json(); - let response_message = ""; + // get response message + let data = await resp.json(); + fill_fetch_response(response, data); + } + catch (error) + { + console.error('Error', error); + } - return { - status: response_status, - message: data.message, - room: data.room, - }; + return response; } export async function join_room(room: Room) @@ -57,21 +66,6 @@ export async function join_room(room: Room) return data.room; } -function set_client_name_on_room(room: Room) -{ - console.log("in set_client_name_on_room, for room:", room); - if (room.type === 'direct') - { - console.log("in direct room"); - room.client_name = room.users[0]; - if (room.client_name === user.username) - room.client_name = room.users[1]; - } - else - room.client_name = room.name; - return room; -} - export async function change_room(room: Room) { console.log("in change_room"); @@ -92,6 +86,37 @@ export async function change_room(room: Room) layout.set("room"); } +export async function send_password(room: Room) +{ + console.log("in create_room"); + + let response: FetchResponse = { status: 0 }; + + // send the new room + try { + const resp = await fetch('/api/v2/chat/create', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(room), + }); + console.log("resp.status:"); + console.log(resp.status); + response.status = resp.status; + if (!resp.ok) + throw new Error(resp.statusText); + + // get response message + let data = await resp.json(); + fill_fetch_response(response, data); + } + catch (error) + { + console.error('Error', error); + } + + return response; +} + export async function invite_user(user_name: string) { console.log("in invite_user"); diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Request_utils.ts b/srcs/requirements/svelte/api_front/src/pieces/chat/Request_utils.ts new file mode 100644 index 00000000..d0378af5 --- /dev/null +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Request_utils.ts @@ -0,0 +1,29 @@ +import { user } from './Store_chat'; +import type { Room, FetchResponse } from './Interface_chat'; + +export function set_client_name_on_room(room: Room) +{ + console.log("in set_client_name_on_room, for room:", room); + if (room.type === 'direct') + { + console.log("in direct room"); + room.client_name = room.users[0]; + if (room.client_name === user.username) + room.client_name = room.users[1]; + } + else + room.client_name = room.name; + return room; +} + +export function fill_fetch_response(response: FetchResponse, data: any) +{ + if (data.display) + response.display = data.display; + if (data.code) + response.code = data.code; + if (data.message) + response.message = data.message; + if (data.room) + response.room = data.room; +}