add html to signup endpoint

This commit is contained in:
2023-07-21 16:59:03 +02:00
parent a943838622
commit b7d87b2fef
4 changed files with 42 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
import { IsEmail, IsOptional, Length } from 'class-validator' import { ApiProperty } from '@nestjs/swagger'
import { IsBoolean, IsEmail, IsOptional, IsString, Length } from 'class-validator'
export class RegistrationDto { export class RegistrationDto {
@Length(3, 30) @Length(3, 30)
@@ -10,4 +11,24 @@ export class RegistrationDto {
@IsEmail() @IsEmail()
email: string 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: `<b>Hello, ##name##!</b><br/>Please confirm your email by clicking on the link below:<br/><a href="http://localhost:3000/confirm-email/##token##">Confirm email</a>. If it doesn't work, copy and paste the following link in your browser:<br/>http://localhost:3000/confirm-email/##token##`,
})
@IsOptional()
@IsString()
html?: string
/** Email subject */
@IsOptional()
@IsString()
subject?: string
/** Whether to send a confirmation email or not.
* Defaults to false
* @example false
*/
@IsOptional()
@IsBoolean()
sendConfirmationEmail?: boolean
} }

View File

@@ -35,10 +35,19 @@ export class CreateUserHandler implements ICommandHandler<CreateUserCommand> {
if (!createdUser) { if (!createdUser) {
return null return null
} }
if (!command.user.sendConfirmationEmail) {
return {
id: createdUser.id,
name: createdUser.name,
email: createdUser.email,
}
}
await this.usersService.sendConfirmationEmail({ await this.usersService.sendConfirmationEmail({
email: createdUser.email, email: createdUser.email,
name: createdUser.name, name: createdUser.name,
verificationToken: verificationToken, verificationToken: verificationToken,
html: command.user.html,
subject: command.user.subject,
}) })
return { return {

View File

@@ -3,8 +3,11 @@ import { Length, Matches } from 'class-validator'
export class CreateUserDto { export class CreateUserDto {
@Length(3, 10) @Length(3, 10)
name: string name: string
@Length(6, 20) @Length(6, 20)
password: string password: string
/** User's email address */
@Matches(/^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/) @Matches(/^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/)
email: string email: string
} }

View File

@@ -32,18 +32,23 @@ export class UsersService {
email, email,
name, name,
verificationToken, verificationToken,
html = `<b>Hello, ##name##!</b><br/>Please confirm your email by clicking on the link below:<br/><a href="http://localhost:3000/confirm-email/##token##">Confirm email</a>. If it doesn't work, copy and paste the following link in your browser:<br/>http://localhost:3000/confirm-email/##token##`,
subject = 'E-mail confirmation',
}: { }: {
email: string email: string
name: string name: string
verificationToken: string verificationToken: string
html?: string
subject?: string
}) { }) {
const htmlFinal = html.replace('##token##', verificationToken)?.replace('##name##', name)
try { try {
await this.emailService.sendMail({ await this.emailService.sendMail({
from: 'andrii <andrii@andrii.es>', from: 'andrii <andrii@andrii.es>',
to: email, to: email,
text: 'hello and welcome, token is: ' + verificationToken, html: htmlFinal,
html: `<b>Hello ${name}!</b><br/>Please confirm your email by clicking on the link below:<br/><a href="http://localhost:3000/confirm-email/${verificationToken}">Confirm email</a>`, subject,
subject: 'E-mail confirmation',
}) })
} catch (e) { } catch (e) {
this.logger.error(e?.message || e) this.logger.error(e?.message || e)