Skip to content

Commit

Permalink
add OTP to welcome email, flow working
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 18, 2024
1 parent 11916a2 commit 7b65be2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions api/src/modules/auth/services/auth.mailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class AuthMailer {
url: resetPasswordUrl,
expiresIn,
type: TEMPLATE_TYPE.WELCOME,
oneTimePassword: welcomeEmailDto.oneTimePassword,
});

await this.emailService.sendMail({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, any> = {
[TEMPLATE_TYPE.WELCOME]: WELCOME_EMAIL_HTML_CONTENT,
[TEMPLATE_TYPE.PASSWORD_RECOVERY]: RESET_PASSWORD_HTML_CONTENT,
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 => `
<h1>Dear User,</h1>
<br/>
<p>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:</p>
<p>Welcome to the TNC Blue Carbon Cost Tool Platform</p>
<br/>
<p><a href="${url}" target="_blank" rel="noopener noreferrer">Secure Password Reset Link</a></p>
<p>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</p>
<p><a href="${url}" target="_blank" rel="noopener noreferrer">Sign Up Link</a></p>
<br/>
<p>This link will direct you to our app to create a new password. For security reasons, this link will expire after ${expiration}.</p>
<p>If you did not request a password reset, please ignore this email; your password will remain the same.</p>
<p>Your one-time password is ${oneTimePassword}</p>
<p>For security reasons, this link will expire after ${expiration}.</p>
<br/>
<p>Thank you for using the platform. We're committed to ensuring your account's security.</p>
<p>Best regards.</p>`;

0 comments on commit 7b65be2

Please sign in to comment.