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

Ojn02/feat sign in error handling #56

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 13 additions & 8 deletions apps/backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ParseIntPipe,
Post,
Request,
UnauthorizedException,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
Expand Down Expand Up @@ -47,17 +48,21 @@ export class AuthController {

// TODO will be deprecated if we use Google OAuth
@Post('/verify')
verifyUser(@Body() body: VerifyUserRequestDTO): void {
try {
this.authService.verifyUser(body.email, String(body.verificationCode));
} catch (e) {
throw new BadRequestException(e.message);
}
async verifyUser(@Body() body: VerifyUserRequestDTO) {
ojn03 marked this conversation as resolved.
Show resolved Hide resolved
return await this.authService
.verifyUser(body.email, String(body.verificationCode))
.catch((err) => {
throw new BadRequestException(err.message);
});
}

@Post('/signin')
signin(@Body() signInDto: SignInRequestDto): Promise<SignInResponseDto> {
return this.authService.signin(signInDto);
async signin(
ojn03 marked this conversation as resolved.
Show resolved Hide resolved
@Body() signInDto: SignInRequestDto,
): Promise<SignInResponseDto> {
return await this.authService.signin(signInDto).catch((err) => {
throw new UnauthorizedException(err.message);
});
}

// TODO implement change/forgotPassword endpoint (service methods are already implemented)
Expand Down
22 changes: 14 additions & 8 deletions apps/backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,22 @@ export class AuthService {

verifyUser(email: string, verificationCode: string): Promise<unknown> {
return new Promise((resolve, reject) => {
return new CognitoUser({
ojn03 marked this conversation as resolved.
Show resolved Hide resolved
const cognitoUser = new CognitoUser({
Username: email,
Pool: this.userPool,
}).confirmRegistration(verificationCode, true, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});

return cognitoUser.confirmRegistration(
verificationCode,
true,
(err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
},
);
});
}

Expand All @@ -108,7 +114,7 @@ export class AuthService {

const cognitoUser = new CognitoUser(userData);

return new Promise<SignInResponseDto>((resolve, reject) => {
ojn03 marked this conversation as resolved.
Show resolved Hide resolved
return new Promise((resolve, reject) => {
return cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (result) => {
resolve({
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/auth/dtos/verify-user.request.dto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { IsEmail, IsNumber } from 'class-validator';
import { IsEmail, IsNumberString } from 'class-validator';

export class VerifyUserRequestDTO {
@IsEmail()
email: string;

@IsNumber()
@IsNumberString()
verificationCode: number;
}
Loading