Skip to content

Commit

Permalink
Merge pull request #18 from fga-eps-mds/72-password-email
Browse files Browse the repository at this point in the history
72 password email
  • Loading branch information
DaviMatheus authored Aug 18, 2024
2 parents 0a78d4a + 45f6b56 commit a6774c7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 34 deletions.
11 changes: 1 addition & 10 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,6 @@ export class AuthController {

@Put('reset-password')
async resetPassword(@Body() resetPasswordDto: ResetPasswordDto) {
if (resetPasswordDto.newPassword !== resetPasswordDto.confirmPassword) {
throw new UnauthorizedException('Passwords are not the same');
}

await this.authService.resetPassword(
resetPasswordDto.newPassword,
resetPasswordDto.resetToken,
);

return { message: 'Password reset successful' };
return this.authService.resetPassword(resetPasswordDto);
}
}
31 changes: 16 additions & 15 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { EmailService } from 'src/users/email.service';
import { ResetToken } from 'src/users/interface/reset-token.schema';
import { Response } from 'express';
import { ConfigService } from '@nestjs/config';
import { ResetPasswordDto } from 'src/users/dtos/reset-password.dto';

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -180,23 +181,23 @@ export class AuthService {
async forgotPassword(email: string) {
const user = await this.userModel.findOne({ email });

if (user) {
const expiryDate = new Date();
expiryDate.setHours(expiryDate.getHours() + 1);

const resetToken = nanoid(64);
await this.ResetTokenModel.create({
token: resetToken,
userId: user._id,
expiryDate,
});
this.emailService.sendPasswordResetEmail(email, resetToken);
if (!user) {
throw new NotFoundException('User not found');
}
const expiryDate = new Date();
expiryDate.setHours(expiryDate.getHours() + 1);
const resetToken = nanoid(64);
await this.ResetTokenModel.create({
token: resetToken,
userId: user._id,
expiryDate,
});
this.emailService.sendPasswordResetEmail(email, resetToken);

return { message: 'If this user exists, they will receive an email' };
return { message: 'Check your email, you will receive an redirect link' };
}

async resetPassword(newPassword: string, resetToken: string) {
async resetPassword({newPassword, resetToken}: ResetPasswordDto) {
const token = await this.ResetTokenModel.findOneAndDelete({
token: resetToken,
expiryDate: { $gte: new Date() },
Expand All @@ -208,10 +209,10 @@ export class AuthService {

const user = await this.userModel.findById(token.userId);
if (!user) {
throw new InternalServerErrorException();
throw new NotFoundException('User not found...');
}

user.password = newPassword;
await user.save();
return { message: 'Password reset successful' };
}
}
7 changes: 0 additions & 7 deletions src/users/dtos/reset-password.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,4 @@ export class ResetPasswordDto {
message: 'Password must contain at least one number',
})
newPassword: string;

@IsString()
@MinLength(6)
@Matches(/^(?=.*[0-9])/, {
message: 'Password must contain at least one number',
})
confirmPassword: string;
}
5 changes: 3 additions & 2 deletions src/users/email.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as nodemailer from 'nodemailer';
import * as sgTransport from 'nodemailer-sendgrid-transport';

@Injectable()
export class EmailService {
private transporter: nodemailer.Transporter;

constructor() {
constructor(private readonly configService: ConfigService) {
this.transporter = nodemailer.createTransport(
sgTransport({
auth: {
Expand Down Expand Up @@ -103,7 +104,7 @@ export class EmailService {
}

async sendPasswordResetEmail(to: string, token: string): Promise<void> {
const resetLink = `http://yourapp.com/reset-password?token=${token}`;
const resetLink = `${this.configService.get<string>('FRONTEND_URL')}/reset-password?token=${token}`;
const mailOptions = {
from: process.env.EMAIL_USER,
to: to,
Expand Down

0 comments on commit a6774c7

Please sign in to comment.