From 7b65be2537f3da33aae7d317c8697dc5c1333060 Mon Sep 17 00:00:00 2001 From: alexeh Date: Fri, 18 Oct 2024 05:40:04 +0200 Subject: [PATCH] add OTP to welcome email, flow working --- api/src/modules/auth/services/auth.mailer.ts | 1 + .../email/templates/email-template.builder.ts | 13 +++++++++++-- .../email/templates/welcome-email.template.ts | 15 ++++++++++----- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/api/src/modules/auth/services/auth.mailer.ts b/api/src/modules/auth/services/auth.mailer.ts index ebf081f8..087489fa 100644 --- a/api/src/modules/auth/services/auth.mailer.ts +++ b/api/src/modules/auth/services/auth.mailer.ts @@ -63,6 +63,7 @@ export class AuthMailer { url: resetPasswordUrl, expiresIn, type: TEMPLATE_TYPE.WELCOME, + oneTimePassword: welcomeEmailDto.oneTimePassword, }); await this.emailService.sendMail({ diff --git a/api/src/modules/notifications/email/templates/email-template.builder.ts b/api/src/modules/notifications/email/templates/email-template.builder.ts index fb078fc6..bc9c9a2f 100644 --- a/api/src/modules/notifications/email/templates/email-template.builder.ts +++ b/api/src/modules/notifications/email/templates/email-template.builder.ts @@ -8,16 +8,24 @@ export enum TEMPLATE_TYPE { EMAIL_UPDATE_CONFIRMATION = 'email-update-confirmation', } -export interface TemplateConfig { +export interface BaseTemplateConfig { url: string; expiresIn: string; type: TEMPLATE_TYPE; + oneTimePassword?: string; } +// If the type is WELCOME, then oneTimePassword is required +export type TemplateConfig = BaseTemplateConfig & + (BaseTemplateConfig['type'] extends TEMPLATE_TYPE.WELCOME + ? { oneTimePassword: string } + : { oneTimePassword?: string }); + export class EmailTemplateBuilder { private readonly url: string; private readonly expiration: string; private readonly type: TEMPLATE_TYPE; + private readonly oneTimePassword?: string; private templateMap: Record = { [TEMPLATE_TYPE.WELCOME]: WELCOME_EMAIL_HTML_CONTENT, [TEMPLATE_TYPE.PASSWORD_RECOVERY]: RESET_PASSWORD_HTML_CONTENT, @@ -30,11 +38,12 @@ export class EmailTemplateBuilder { contentConfig.expiresIn, ); this.type = contentConfig.type; + this.oneTimePassword = contentConfig.oneTimePassword; } build() { const template = this.templateMap[this.type]; - return template(this.url, this.expiration); + return template(this.url, this.expiration, this.oneTimePassword); } private passwordRecoveryTokenExpirationHumanReadable(expirationIn: string) { diff --git a/api/src/modules/notifications/email/templates/welcome-email.template.ts b/api/src/modules/notifications/email/templates/welcome-email.template.ts index df52a50c..b63d052b 100644 --- a/api/src/modules/notifications/email/templates/welcome-email.template.ts +++ b/api/src/modules/notifications/email/templates/welcome-email.template.ts @@ -1,12 +1,17 @@ -export const WELCOME_EMAIL_HTML_CONTENT = (url, expiration): string => ` +export const WELCOME_EMAIL_HTML_CONTENT = ( + url: string, + expiration: string, + oneTimePassword: string, +): string => `

Dear User,


-

We recently received a request to reset your password for your account. If you made this request, please click on the link below to securely change your password:

+

Welcome to the TNC Blue Carbon Cost Tool Platform


-

Secure Password Reset Link

+

Thank you for signing up. We're excited to have you on board. Please active you account by signing up adding a password of your choice

+

Sign Up Link


-

This link will direct you to our app to create a new password. For security reasons, this link will expire after ${expiration}.

-

If you did not request a password reset, please ignore this email; your password will remain the same.

+

Your one-time password is ${oneTimePassword}

+

For security reasons, this link will expire after ${expiration}.


Thank you for using the platform. We're committed to ensuring your account's security.

Best regards.

`;