From c803b92295738abe82306dd5bc9926c7736fde0e Mon Sep 17 00:00:00 2001 From: Romain MOREL Date: Mon, 20 Nov 2023 12:59:19 +0100 Subject: [PATCH 1/3] Fixed 2FA Login problem --- front/src/components/TwoFaLoginInput.tsx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/front/src/components/TwoFaLoginInput.tsx b/front/src/components/TwoFaLoginInput.tsx index 72b7a50..9d301e6 100644 --- a/front/src/components/TwoFaLoginInput.tsx +++ b/front/src/components/TwoFaLoginInput.tsx @@ -10,12 +10,10 @@ import { Button } from './Button'; const loginWithTwoFaCode = async ({ code, - login, }: { code: string; - login: string | undefined; }): Promise => { - const json = await api.post('auth/2fa/login', { json: { twoFACode: code, login: login } }).json(); + const json = await api.post('auth/2fa/login', { json: { twoFACode: code } }).json(); const token: tokenDto = json as tokenDto; return token; }; @@ -53,7 +51,7 @@ export const TwoFaLoginInput = (props: any) => { From 6268dd82122b00026be8608ab3fdf79a7afad1ff Mon Sep 17 00:00:00 2001 From: Romain MOREL Date: Mon, 20 Nov 2023 13:15:10 +0100 Subject: [PATCH 2/3] Optimizing auth.service loginWith2FA --- api/src/modules/auth/auth.controller.ts | 4 ++-- api/src/modules/auth/auth.dto.ts | 4 ---- api/src/modules/auth/auth.service.ts | 7 ++----- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/api/src/modules/auth/auth.controller.ts b/api/src/modules/auth/auth.controller.ts index 515e572..ba5f19c 100644 --- a/api/src/modules/auth/auth.controller.ts +++ b/api/src/modules/auth/auth.controller.ts @@ -30,8 +30,8 @@ export class AuthController { @Post('2fa/login') @UseGuards(JwtAuthGuard) - async connectWith2FA(@Body() body: loginTwoFaDto) { - const token = await this.authService.loginWithTwoFa(body.twoFACode, body.login); + async connectWith2FA(@Body() body: loginTwoFaDto, @GetUser() user: User) { + const token = await this.authService.loginWithTwoFa(body.twoFACode, user); return { token }; } diff --git a/api/src/modules/auth/auth.dto.ts b/api/src/modules/auth/auth.dto.ts index eaf8bca..58362a5 100644 --- a/api/src/modules/auth/auth.dto.ts +++ b/api/src/modules/auth/auth.dto.ts @@ -47,8 +47,4 @@ export class loginTwoFaDto { @IsNotEmpty() @IsString() twoFACode: string; - - @IsNotEmpty() - @IsString() - login: string; } diff --git a/api/src/modules/auth/auth.service.ts b/api/src/modules/auth/auth.service.ts index d22550d..d83f7ca 100644 --- a/api/src/modules/auth/auth.service.ts +++ b/api/src/modules/auth/auth.service.ts @@ -48,10 +48,7 @@ export class AuthService { return { token, isTwoFaEnabled }; } - async loginWithTwoFa(code: string, login: string) { - // user is supposed to be created at this point - const user = await this.userService.getUnique(login); - + async loginWithTwoFa(code: string, user: User) { const isCodeValid = this.isTwoFactorAuthCodeValid(code, user); if (!isCodeValid) { throw new UnauthorizedException('Wrong 2FA code'); @@ -64,7 +61,7 @@ export class AuthService { }; const token = this.generateJWT(payload); - this.logger.log(`${login} logged in with 2FA`); + this.logger.log(`${user.login} logged in with 2FA`); return token; } From 51ce9b2957080ac6f63287da3c19973332a9f326 Mon Sep 17 00:00:00 2001 From: B-ki Date: Mon, 20 Nov 2023 12:17:09 +0000 Subject: [PATCH 3/3] Apply automatic changes --- front/src/components/TwoFaLoginInput.tsx | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/front/src/components/TwoFaLoginInput.tsx b/front/src/components/TwoFaLoginInput.tsx index 9d301e6..63cee9c 100644 --- a/front/src/components/TwoFaLoginInput.tsx +++ b/front/src/components/TwoFaLoginInput.tsx @@ -8,11 +8,7 @@ import { api, setApiToken } from '@/utils/api'; import { Button } from './Button'; -const loginWithTwoFaCode = async ({ - code, -}: { - code: string; -}): Promise => { +const loginWithTwoFaCode = async ({ code }: { code: string }): Promise => { const json = await api.post('auth/2fa/login', { json: { twoFACode: code } }).json(); const token: tokenDto = json as tokenDto; return token; @@ -48,11 +44,7 @@ export const TwoFaLoginInput = (props: any) => {
-