feat: implement configs and email template configs

This commit is contained in:
Kakious 2024-07-27 01:26:16 -04:00
parent 3608f91381
commit 5db3ed1806
4 changed files with 54 additions and 7 deletions

View file

View file

View file

@ -2,6 +2,7 @@ export default async () => ({
base: { base: {
localUrl: `${process.env.BASE_URL}`, localUrl: `${process.env.BASE_URL}`,
fromEmail: `${process.env.FROM_EMAIL}`, fromEmail: `${process.env.FROM_EMAIL}`,
appName: `${process.env.APP_NAME ?? 'Waterwolf Auth'}`,
}, },
redis: { redis: {
host: process.env['REDIS_HOST'] ?? 'localhost', host: process.env['REDIS_HOST'] ?? 'localhost',

View file

@ -18,29 +18,60 @@ export class MailService {
* Send a password reset email to the user * Send a password reset email to the user
* @param email The email address of the user * @param email The email address of the user
*/ */
public async sendPasswordResetEmail(email: string, resetCode: string): Promise<void> {} public async sendPasswordResetEmail(email: string, resetCode: string): Promise<void> {
this.logger.debug(`Sending password reset email to ${email}`);
const resetUrl = `${this.configService.getOrThrow('base.localUrl')}/auth/reset-password?code=${resetCode}`;
const { html: templateHtml, text: templateText } = await this.fetchTemplate('reset-password');
const html = Handlebars.compile(templateHtml)({ resetUrl });
const text = Handlebars.compile(templateText)({ resetUrl });
this.postalService.sendMessage({
to: [email],
subject: 'Reset your password',
from: this.configService.getOrThrow('MAIL_FROM'),
plain_body: text,
html_body: html,
});
}
/** /**
* Send a welcome email to the user * Send a welcome email to the user
* @param email The email address of the user * @param email The email address of the user
*/ */
public async sendWelcomeEmail(email: string): Promise<void> {} public async sendWelcomeEmail(email: string): Promise<void> {
this.logger.debug(`Sending verification email to ${email}`);
const { html: templateHtml, text: templateText } = await this.fetchTemplate('welcome');
const html = Handlebars.compile(templateHtml)({});
const text = Handlebars.compile(templateText)({});
this.postalService.sendMessage({
to: [email],
subject: `Welcome to ${this.configService.getOrThrow('base.appName')}`,
from: this.configService.getOrThrow('MAIL_FROM'),
plain_body: text,
html_body: html,
});
}
/** /**
* Send a verification email to the user * Send a verification email to the user
* @param email The email address of the user * @param email The email address of the user
*/ */
public async sendVerificationEmail(email: string, code: string): Promise<void> { public async sendVerificationEmail(email: string, code: string): Promise<void> {
this.logger.debug(`Sending welcome email to ${email}`); this.logger.debug(`Sending verification email to ${email}`);
const verificationUrl = `${this.configService.getOrThrow('BASE_URL')}/auth/verify?code=${code}`; const verificationUrl = `${this.configService.getOrThrow('base.localUrl')}/auth/verify?code=${code}`;
//handlebar render email template //handlebar render email template
const { html: templateHtml, text: templateText } = await this.fetchTemplate('verify-email'); const { html: templateHtml, text: templateText } = await this.fetchTemplate('verify-email');
//replace the placeholders with the actual values //replace the placeholders with the actual values
const template = Handlebars.compile(templateHtml); const html = Handlebars.compile(templateHtml)({ verificationUrl });
const html = template({ verificationUrl });
const text = Handlebars.compile(templateText)({ verificationUrl }); const text = Handlebars.compile(templateText)({ verificationUrl });
this.postalService.sendMessage({ this.postalService.sendMessage({
@ -56,7 +87,22 @@ export class MailService {
* Send a password changed email to the user * Send a password changed email to the user
* @param email The email address of the user * @param email The email address of the user
*/ */
public async sendPasswordChangedEmail(email: string): Promise<void> {} public async sendPasswordChangedEmail(email: string): Promise<void> {
this.logger.debug(`Sending password changed email to ${email}`);
const { html: templateHtml, text: templateText } = await this.fetchTemplate('password-changed');
const html = Handlebars.compile(templateHtml)({});
const text = Handlebars.compile(templateText)({});
this.postalService.sendMessage({
to: [email],
subject: 'Password changed',
from: this.configService.getOrThrow('MAIL_FROM'),
plain_body: text,
html_body: html,
});
}
/** /**
* Private function. Reads the html email template from the file system and returns the content * Private function. Reads the html email template from the file system and returns the content