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

forgot password / confirm reset password flow #51

Merged
merged 3 commits into from
Mar 12, 2024
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
28 changes: 24 additions & 4 deletions apps/backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { User } from '../users/user.entity';
import { SignInResponseDto } from './dtos/sign-in.response.dto';
import { CurrentUserInterceptor } from '../interceptors/current-user.interceptor';
import { AuthGuard } from '@nestjs/passport';
import { ForgotPasswordRequestDto } from './dtos/forgot-password.request.dto';
import { ConfirmResetPasswordDto } from './dtos/confirm-reset-password.request.dto';
import { UserStatus } from '../users/types';

@Controller('auth')
Expand Down Expand Up @@ -72,10 +74,6 @@ export class AuthController {
return this.authService.signin(signInDto);
}

// TODO implement change/forgotPassword endpoint (service methods are already implemented)
// But this won't be necessary if we use Google OAuth
// https://dev.to/fstbraz/authentication-with-aws-cognito-passport-and-nestjs-part-iii-2da5

@Post('/delete/:userId')
@UseGuards(AuthGuard('jwt'))
async delete(
Expand All @@ -96,4 +94,26 @@ export class AuthController {

this.usersService.remove(req.user, user.id);
}

@Post('/forgotPassword')
async forgotPassword(@Body() body: ForgotPasswordRequestDto) {
try {
await this.authService.forgotPassword(body.email);
} catch (e) {
throw new BadRequestException(e.message);
}
}

@Post('/confirmResetPassword')
async confirmResetPassword(@Body() body: ConfirmResetPasswordDto) {
try {
await this.authService.confirmPassword(
body.email,
body.verificationCode,
body.newPassword,
);
} catch (e) {
throw new BadRequestException(e.message);
}
}
}
2 changes: 0 additions & 2 deletions apps/backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export class AuthService {
});
}

// TODO not currently used
forgotPassword(email: string): Promise<unknown> {
return new Promise((resolve, reject) => {
return new CognitoUser({
Expand All @@ -140,7 +139,6 @@ export class AuthService {
});
}

// TODO not currently used
confirmPassword(
email: string,
verificationCode: string,
Expand Down
12 changes: 12 additions & 0 deletions apps/backend/src/auth/dtos/confirm-reset-password.request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { IsEmail, IsNumberString, IsString } from 'class-validator';

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

@IsNumberString()
verificationCode: string;

@IsString()
newPassword: string;
}
6 changes: 6 additions & 0 deletions apps/backend/src/auth/dtos/forgot-password.request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IsEmail } from 'class-validator';

export class ForgotPasswordRequestDto {
@IsEmail()
email: string;
}
Loading