-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
45 lines (41 loc) · 1007 Bytes
/
index.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
'use strict';
/* Utility function to return a working delivery module (email)
*
* Deliver module API:
* async (user, token, req) => {}
*
*/
const email = require('emailjs');
const { promisifyAll } = require('../utils/promise');
const defaultTemplate = ({ user, token, tokenField, req }) => {
const { protocol } = req;
const hostname = (req.get && req.get('host')) || req.hostname;
return {
subject: `Token for ${hostname}!`,
text: `Hello!\nAccess your account here: ${protocol}://${hostname}/?${tokenField}=${token}`
};
};
module.exports = ({
template = defaultTemplate,
tokenField = 'token',
from,
...smtpConfig
}) => {
const smtpServer = promisifyAll(
email.server.connect({
ssl: true,
...smtpConfig
})
);
return {
tokenField,
addressField: 'email',
async send(user, token, req) {
await smtpServer.sendAsync({
to: user.email,
from,
...template({ user, token, tokenField, req })
});
}
};
};