From 75361abc82c3e16036d48d0a67459f62453333d1 Mon Sep 17 00:00:00 2001 From: Andres Date: Tue, 18 Jul 2023 12:53:39 +0200 Subject: [PATCH] add html and subject to password recovery props --- src/modules/auth/auth.controller.ts | 2 +- src/modules/auth/dto/recover-password.dto.ts | 18 +++++++++++++++++- .../send-password-recovery-email-use-case.ts | 7 +++++-- src/modules/users/services/users.service.ts | 12 ++++++++++-- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index 11a42ed..cb94a32 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -208,7 +208,7 @@ export class AuthController { @HttpCode(HttpStatus.NO_CONTENT) @Post('recover-password') async recoverPassword(@Body() body: RecoverPasswordDto): Promise { - return await this.commandBus.execute(new SendPasswordRecoveryEmailCommand(body.email)) + return await this.commandBus.execute(new SendPasswordRecoveryEmailCommand(body)) } @ApiOperation({ description: 'Reset password', summary: 'Reset password' }) diff --git a/src/modules/auth/dto/recover-password.dto.ts b/src/modules/auth/dto/recover-password.dto.ts index 70c7bae..fee0ff0 100644 --- a/src/modules/auth/dto/recover-password.dto.ts +++ b/src/modules/auth/dto/recover-password.dto.ts @@ -1,6 +1,22 @@ -import { IsEmail } from 'class-validator' +import { ApiProperty } from '@nestjs/swagger' +import { IsEmail, IsOptional, IsString } from 'class-validator' export class RecoverPasswordDto { + /** User's email address */ @IsEmail() email: string + + @ApiProperty({ + description: `HTML template to be sent in the email;\n ##name## will be replaced with the user's name; \n ##token## will be replaced with the password recovery token`, + example: + '

Hi, ##name#

Click here to recover your password

', + }) + @IsString() + @IsOptional() + html?: string + + /** Email subject */ + @IsString() + @IsOptional() + subject?: string } diff --git a/src/modules/auth/use-cases/send-password-recovery-email-use-case.ts b/src/modules/auth/use-cases/send-password-recovery-email-use-case.ts index 85a1f56..52fb2ca 100644 --- a/src/modules/auth/use-cases/send-password-recovery-email-use-case.ts +++ b/src/modules/auth/use-cases/send-password-recovery-email-use-case.ts @@ -4,9 +4,10 @@ import { v4 as uuidv4 } from 'uuid' import { UsersRepository } from '../../users/infrastructure/users.repository' import { UsersService } from '../../users/services/users.service' +import { RecoverPasswordDto } from '../dto' export class SendPasswordRecoveryEmailCommand { - constructor(public readonly email: string) {} + constructor(public readonly body: RecoverPasswordDto) {} } @CommandHandler(SendPasswordRecoveryEmailCommand) @@ -19,7 +20,7 @@ export class SendPasswordRecoveryEmailHandler ) {} async execute(command: SendPasswordRecoveryEmailCommand) { - const user = await this.usersRepository.findUserByEmail(command.email) + const user = await this.usersRepository.findUserByEmail(command.body.email) if (!user) { throw new NotFoundException('User not found') @@ -30,7 +31,9 @@ export class SendPasswordRecoveryEmailHandler await this.usersService.sendPasswordRecoveryEmail({ email: updatedUser.user.email, name: updatedUser.user.name, + html: command.body.html, passwordRecoveryToken: updatedUser.resetPasswordToken, + subject: command.body.subject, }) if (!updatedUser) { throw new NotFoundException('User not found') diff --git a/src/modules/users/services/users.service.ts b/src/modules/users/services/users.service.ts index 28cfe8f..7bc092a 100644 --- a/src/modules/users/services/users.service.ts +++ b/src/modules/users/services/users.service.ts @@ -56,17 +56,25 @@ export class UsersService { email, name, passwordRecoveryToken, + html, + subject, }: { email: string name: string + html?: string + subject?: string passwordRecoveryToken: string }) { + const htmlFinal = + html.replace('##token##', passwordRecoveryToken).replace('##name##', name) || + `Hello ${name}!
To recover your password follow this link:
Confirm email. If it doesn't work, copy and paste the following link in your browser:
http://localhost:3000/confirm-email/${passwordRecoveryToken} ` + try { await this.emailService.sendMail({ from: 'Andrii ', to: email, - html: `Hello ${name}!
To recover your password follow this link:
Confirm email. If it doesn't work, copy and paste the following link in your browser:
http://localhost:3000/confirm-email/${passwordRecoveryToken} `, - subject: 'Password recovery', + html: htmlFinal, + subject: subject || 'Password recovery', }) } catch (e) { this.logger.error(e?.message || e)