Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
Add email body templates
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXyfir committed Mar 26, 2019
1 parent 28934b1 commit 9d511f4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
24 changes: 21 additions & 3 deletions server/lib/login/passwordless/start.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { buildTemplate } from 'lib/email/build-template';
import { emailToId } from 'lib/email/to-id';
import { sendMail } from 'lib/email/send';
import * as storage from 'node-persist';
import { Accownt } from 'types/accownt';
import { signJWT } from 'lib/jwt/sign';

const { TEMP_JWT_EXPIRES_IN, ACCOWNT_API_URL, STORAGE, NAME } = process.enve;
const {
PASSWORDLESS_LOGIN_HTML_TEMPLATE,
PASSWORDLESS_LOGIN_TEXT_TEMPLATE,
TEMP_JWT_EXPIRES_IN,
ACCOWNT_API_URL,
STORAGE,
NAME
} = process.enve;

export async function startPasswordlessLogin(
email: Accownt.User['email']
Expand All @@ -17,8 +25,18 @@ export async function startPasswordlessLogin(
const link = `${ACCOWNT_API_URL}/login/passwordless?jwt=${token}`;
await sendMail({
subject: `${NAME} Passwordless Login`,
html: `<a href="${link}">Login to ${NAME}.</a>`,
text: link,
html: await buildTemplate({
name: 'LINK',
file: PASSWORDLESS_LOGIN_HTML_TEMPLATE,
value: link,
fallback: '<a href="%LINK%">Login.</a>'
}),
text: await buildTemplate({
name: 'LINK',
file: PASSWORDLESS_LOGIN_TEXT_TEMPLATE,
value: link,
fallback: 'Login: %LINK%"'
}),
to: user.email
});

Expand Down
17 changes: 15 additions & 2 deletions server/lib/register/start.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { buildTemplate } from 'lib/email/build-template';
import { setPassword } from 'lib/account/set-password';
import { checkEmail } from 'lib/register/check-email';
import { cleanEmail } from 'lib/email/clean';
Expand All @@ -9,6 +10,8 @@ import * as qs from 'qs';
import axios from 'axios';

const {
EMAIL_VERIFICATION_HTML_TEMPLATE,
EMAIL_VERIFICATION_TEXT_TEMPLATE,
TEMP_JWT_EXPIRES_IN,
ACCOWNT_API_URL,
RECAPTCHA_KEY,
Expand Down Expand Up @@ -56,8 +59,18 @@ export async function startRegistration(
const link = `${ACCOWNT_API_URL}/register?jwt=${token}`;
await sendMail({
subject: `${NAME} Email Verification`,
html: `<a href="${link}">Verify my email.</a>`,
text: link,
html: await buildTemplate({
name: 'LINK',
file: EMAIL_VERIFICATION_HTML_TEMPLATE,
value: link,
fallback: '<a href="%LINK%">Verify my email.</a>'
}),
text: await buildTemplate({
name: 'LINK',
file: EMAIL_VERIFICATION_TEXT_TEMPLATE,
value: link,
fallback: 'Verify your email: %LINK%"'
}),
to: email
});

Expand Down
26 changes: 26 additions & 0 deletions types/accownt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,32 @@ export namespace Accownt {
* @example "1h"
*/
TEMP_JWT_EXPIRES_IN: string;
/**
* Path to a file containing the template for the text-only version of
* the email/account verification message sent out after registration.
* Instances of `%LINK%` will be replaced with the verification link.
* @example "/path/to/templates/email-verification.txt" // Value
* @example "Verify your account here: %LINK%" // File contents
*/
EMAIL_VERIFICATION_TEXT_TEMPLATE?: string;
/**
* Path to a file containing the template for the HTML version of
* the email/account verification message sent out after registration.
* Instances of `%LINK%` will be replaced with the verification link.
* @example "/path/to/templates/email-verification.html" // Value
* @example '<a href="%LINK">Verify your account</a>' // File contents
*/
EMAIL_VERIFICATION_HTML_TEMPLATE?: string;
/**
* Same as `EMAIL_VERIFICATION_TEXT_TEMPLATE` but for the passwordless
* login email.
*/
PASSWORDLESS_LOGIN_TEXT_TEMPLATE?: string;
/**
* Same as `EMAIL_VERIFICATION_HTML_TEMPLATE` but for the passwordless
* login email.
*/
PASSWORDLESS_LOGIN_HTML_TEMPLATE?: string;
}

export interface Web extends Accownt.Env.Common {
Expand Down

0 comments on commit 9d511f4

Please sign in to comment.