-
Notifications
You must be signed in to change notification settings - Fork 0
/
verificationUtil.js
77 lines (69 loc) · 3.11 KB
/
verificationUtil.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import sgMail from '@sendgrid/mail';
// Email HTML body
const html = ({ url, email }) => {
// Insert invisible space into domains and email address to prevent both the
// email address and the domain from being turned into a hyperlink by email
// clients like Outlook and Apple mail, as this is confusing because it seems
// like they are supposed to click on their email address to sign in.
const escapedEmail = `${email.replace(/\./g, '​.')}`;
const escapedSite = `${process.env.SITE_NAME.replace(/\./g, '​.')}`;
// Some simple styling options
const backgroundColor = '#f9f9f9';
const textColor = '#444444';
const mainBackgroundColor = '#ffffff';
const buttonBackgroundColor = '#346df1';
const buttonBorderColor = '#346df1';
const buttonTextColor = '#ffffff';
// Uses tables for layout and inline CSS due to email client limitations
return `
<body style="background: ${backgroundColor};">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" style="padding: 10px 0px 20px 0px; font-size: 22px; font-family: Helvetica, Arial, sans-serif; color: ${textColor};">
<strong>${escapedSite}</strong>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="20" cellpadding="0" style="background: ${mainBackgroundColor}; max-width: 600px; margin: auto; border-radius: 10px;">
<tr>
<td align="center" style="padding: 10px 0px 0px 0px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; color: ${textColor};">
Sign in as <strong>${escapedEmail}</strong>
</td>
</tr>
<tr>
<td align="center" style="padding: 20px 0;">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" style="border-radius: 5px;" bgcolor="${buttonBackgroundColor}"><a href="${url}" target="_blank" style="font-size: 18px; font-family: Helvetica, Arial, sans-serif; color: ${buttonTextColor}; text-decoration: none; text-decoration: none;border-radius: 5px; padding: 10px 20px; border: 1px solid ${buttonBorderColor}; display: inline-block; font-weight: bold;">Sign in</a></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center" style="padding: 0px 0px 10px 0px; font-size: 16px; line-height: 22px; font-family: Helvetica, Arial, sans-serif; color: ${textColor};">
If you did not request this email you can safely ignore it.
</td>
</tr>
</table>
</body>
`;
};
// Email text body – fallback for email clients that don't render HTML
const text = ({ url }) => `Sign in to ${process.env.SITE_NAME}\n${url}\n\n`;
const sendVerificationRequestOverride = ({
identifier: email, url, provider,
}) => new Promise((resolve, reject) => {
const { server, from } = provider;
sgMail.setApiKey(server.auth.pass);
sgMail
.send({
to: email,
from,
subject: `Sign in to ${process.env.SITE_NAME}`,
text: text({ url, email }),
html: html({ url, email }),
})
.then(() => resolve())
.catch((error) => reject(new Error('SEND_VERIFICATION_EMAIL_ERROR', error)));
});
export default sendVerificationRequestOverride;