Skip to content

Commit

Permalink
switch to admininitiateauth; fix refresh tokens (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
huang0h committed Oct 13, 2024
1 parent e17079c commit d606020
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
10 changes: 3 additions & 7 deletions apps/backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class AuthController {
@Post('/verify')
verifyUser(@Body() body: VerifyUserDto): void {
try {
this.authService.verifyUser(body.email, String(body.verificationCode));
this.authService.verifyUser(body.email, body.verificationCode);
} catch (e) {
throw new BadRequestException(e.message);
}
Expand All @@ -60,13 +60,9 @@ export class AuthController {
return this.authService.signin(signInDto);
}

@UseGuards(AuthGuard('jwt'))
@Post('/refresh')
refresh(
@Body() refreshDto: RefreshTokenDto,
@Request() request,
): Promise<SignInResponseDto> {
return this.authService.refreshToken(refreshDto, request.user.idUser);
refresh(@Body() refreshDto: RefreshTokenDto): Promise<SignInResponseDto> {
return this.authService.refreshToken(refreshDto);
}

@Post('/forgotPassword')
Expand Down
18 changes: 10 additions & 8 deletions apps/backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Injectable } from '@nestjs/common';
import {
AdminDeleteUserCommand,
AdminInitiateAuthCommand,
AttributeType,
CognitoIdentityProviderClient,
ConfirmForgotPasswordCommand,
ConfirmSignUpCommand,
ForgotPasswordCommand,
InitiateAuthCommand,
ListUsersCommand,
SignUpCommand,
} from '@aws-sdk/client-cognito-identity-provider';
Expand Down Expand Up @@ -98,9 +98,10 @@ export class AuthService {
}

async signin({ email, password }: SignInDto): Promise<SignInResponseDto> {
const signInCommand = new InitiateAuthCommand({
AuthFlow: 'USER_PASSWORD_AUTH',
const signInCommand = new AdminInitiateAuthCommand({
AuthFlow: 'ADMIN_USER_PASSWORD_AUTH',
ClientId: CognitoAuthConfig.clientId,
UserPoolId: CognitoAuthConfig.userPoolId,
AuthParameters: {
USERNAME: email,
PASSWORD: password,
Expand All @@ -118,13 +119,14 @@ export class AuthService {
}

// Refresh token hash uses a user's sub (unique ID), not their username (typically their email)
async refreshToken(
{ refreshToken }: RefreshTokenDto,
userSub: string,
): Promise<SignInResponseDto> {
const refreshCommand = new InitiateAuthCommand({
async refreshToken({
refreshToken,
userSub,
}: RefreshTokenDto): Promise<SignInResponseDto> {
const refreshCommand = new AdminInitiateAuthCommand({
AuthFlow: 'REFRESH_TOKEN_AUTH',
ClientId: CognitoAuthConfig.clientId,
UserPoolId: CognitoAuthConfig.userPoolId,
AuthParameters: {
REFRESH_TOKEN: refreshToken,
SECRET_HASH: this.calculateHash(userSub),
Expand Down
3 changes: 3 additions & 0 deletions apps/backend/src/auth/dtos/refresh-token.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ import { IsString } from 'class-validator';
export class RefreshTokenDto {
@IsString()
refreshToken: string;

@IsString()
userSub: string;
}
6 changes: 3 additions & 3 deletions apps/backend/src/auth/dtos/verify-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { IsEmail, IsNumber } from 'class-validator';
import { IsEmail, IsString } from 'class-validator';

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

@IsNumber()
verificationCode: number;
@IsString()
verificationCode: string;
}

0 comments on commit d606020

Please sign in to comment.