mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-16 12:33:17 +00:00
add html and subject to password recovery props
This commit is contained in:
@@ -208,7 +208,7 @@ export class AuthController {
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
@Post('recover-password')
|
||||
async recoverPassword(@Body() body: RecoverPasswordDto): Promise<void> {
|
||||
return await this.commandBus.execute(new SendPasswordRecoveryEmailCommand(body.email))
|
||||
return await this.commandBus.execute(new SendPasswordRecoveryEmailCommand(body))
|
||||
}
|
||||
|
||||
@ApiOperation({ description: 'Reset password', summary: 'Reset password' })
|
||||
|
||||
@@ -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:
|
||||
'<h1>Hi, ##name#</h1><p>Click <a href="##token##">here</a> to recover your password</p>',
|
||||
})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
html?: string
|
||||
|
||||
/** Email subject */
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
subject?: string
|
||||
}
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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) ||
|
||||
`<b>Hello ${name}!</b><br/>To recover your password follow this link:<br/><a href="http://localhost:3000/confirm-email/${passwordRecoveryToken}">Confirm email</a>. If it doesn't work, copy and paste the following link in your browser:<br/>http://localhost:3000/confirm-email/${passwordRecoveryToken} `
|
||||
|
||||
try {
|
||||
await this.emailService.sendMail({
|
||||
from: 'Andrii <andrii@andrii.es>',
|
||||
to: email,
|
||||
html: `<b>Hello ${name}!</b><br/>To recover your password follow this link:<br/><a href="http://localhost:3000/confirm-email/${passwordRecoveryToken}">Confirm email</a>. If it doesn't work, copy and paste the following link in your browser:<br/>http://localhost:3000/confirm-email/${passwordRecoveryToken} `,
|
||||
subject: 'Password recovery',
|
||||
html: htmlFinal,
|
||||
subject: subject || 'Password recovery',
|
||||
})
|
||||
} catch (e) {
|
||||
this.logger.error(e?.message || e)
|
||||
|
||||
Reference in New Issue
Block a user