Skip to content

Commit

Permalink
error handling for auth/verify endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ojn03 committed Mar 2, 2024
1 parent 90a5175 commit 541a07b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
16 changes: 9 additions & 7 deletions apps/backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ 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) {
return this.authService
.verifyUser(body.email, String(body.verificationCode))
.catch((err) => {
throw new BadRequestException(err.message);
});
}

@Post('/signin')
signin(@Body() signInDto: SignInRequestDto): Promise<SignInResponseDto> {
async signin(
@Body() signInDto: SignInRequestDto,
): Promise<SignInResponseDto> {
return this.authService.signin(signInDto).catch((err) => {
throw new UnauthorizedException(err.message);
});
Expand Down
23 changes: 14 additions & 9 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({
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) => {
return new Promise((resolve, reject) => {
return cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (result) => {
resolve({
Expand All @@ -117,7 +123,6 @@ export class AuthService {
});
},
onFailure: (err) => {
console.log(err);
reject(err);
},
});
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;
}

0 comments on commit 541a07b

Please sign in to comment.