Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix2 fa login #68

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

Expand Down
4 changes: 0 additions & 4 deletions api/src/modules/auth/auth.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,4 @@ export class loginTwoFaDto {
@IsNotEmpty()
@IsString()
twoFACode: string;

@IsNotEmpty()
@IsString()
login: string;
}
7 changes: 2 additions & 5 deletions api/src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
}
Expand Down
16 changes: 3 additions & 13 deletions front/src/components/TwoFaLoginInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ import { api, setApiToken } from '@/utils/api';

import { Button } from './Button';

const loginWithTwoFaCode = async ({
code,
login,
}: {
code: string;
login: string | undefined;
}): Promise<tokenDto> => {
const json = await api.post('auth/2fa/login', { json: { twoFACode: code, login: login } }).json();
const loginWithTwoFaCode = async ({ code }: { code: string }): Promise<tokenDto> => {
const json = await api.post('auth/2fa/login', { json: { twoFACode: code } }).json();
const token: tokenDto = json as tokenDto;
return token;
};
Expand Down Expand Up @@ -50,11 +44,7 @@ export const TwoFaLoginInput = (props: any) => {
<form onSubmit={handleSubmit} className="flex flex-col items-center">
<input type="test" name="2FAcode" onChange={handleCodeChange} />
</form>
<Button
size="small"
type="primary"
onClick={() => mutation.mutateAsync({ code: code, login: login })}
>
<Button size="small" type="primary" onClick={() => mutation.mutateAsync({ code: code })}>
Submit
</Button>
</>
Expand Down