2019-09-03 06:07:03 +03:00
|
|
|
/**
|
|
|
|
* @typedef { import('nodemailer').Transporter } MailTransporter
|
|
|
|
* @typedef { import('nodemailer').SentMessageInfo } SentMessageInfo
|
|
|
|
* @typedef { string } URL
|
|
|
|
*/
|
|
|
|
|
2020-09-17 17:52:27 +03:00
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @template D
|
|
|
|
* @typedef {Object} TokenProvider<T, D>
|
|
|
|
* @prop {(data: D) => Promise<T>} create
|
|
|
|
* @prop {(token: T) => Promise<D>} validate
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MagicLink
|
|
|
|
* @template Token
|
|
|
|
* @template TokenData
|
|
|
|
*/
|
2020-09-17 17:42:01 +03:00
|
|
|
class MagicLink {
|
|
|
|
/**
|
|
|
|
* @param {object} options
|
|
|
|
* @param {MailTransporter} options.transporter
|
2020-09-17 17:52:27 +03:00
|
|
|
* @param {TokenProvider<Token, TokenData>} options.tokenProvider
|
|
|
|
* @param {(token: Token, type: string) => URL} options.getSigninURL
|
2020-09-17 17:42:01 +03:00
|
|
|
* @param {typeof defaultGetText} [options.getText]
|
|
|
|
* @param {typeof defaultGetHTML} [options.getHTML]
|
|
|
|
* @param {typeof defaultGetSubject} [options.getSubject]
|
|
|
|
*/
|
|
|
|
constructor(options) {
|
2020-09-17 17:52:27 +03:00
|
|
|
if (!options || !options.transporter || !options.tokenProvider || !options.getSigninURL) {
|
|
|
|
throw new Error('Missing options. Expects {transporter, tokenProvider, getSigninURL}');
|
2020-09-17 17:42:01 +03:00
|
|
|
}
|
|
|
|
this.transporter = options.transporter;
|
2020-09-17 17:52:27 +03:00
|
|
|
this.tokenProvider = options.tokenProvider;
|
2020-09-17 17:42:01 +03:00
|
|
|
this.getSigninURL = options.getSigninURL;
|
|
|
|
this.getText = options.getText || defaultGetText;
|
|
|
|
this.getHTML = options.getHTML || defaultGetHTML;
|
|
|
|
this.getSubject = options.getSubject || defaultGetSubject;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sendMagicLink
|
|
|
|
*
|
|
|
|
* @param {object} options
|
|
|
|
* @param {string} options.email - The email to send magic link to
|
2020-09-17 17:52:27 +03:00
|
|
|
* @param {TokenData} options.tokenData - The data for token
|
2020-09-17 17:42:01 +03:00
|
|
|
* @param {string=} [options.type='signin'] - The type to be passed to the url and content generator functions
|
2020-09-17 17:52:27 +03:00
|
|
|
* @returns {Promise<{token: Token, info: SentMessageInfo}>}
|
2020-09-17 17:42:01 +03:00
|
|
|
*/
|
|
|
|
async sendMagicLink(options) {
|
2020-09-17 17:52:27 +03:00
|
|
|
const token = await this.tokenProvider.create(options.tokenData);
|
2020-09-17 17:42:01 +03:00
|
|
|
|
|
|
|
const type = options.type || 'signin';
|
|
|
|
|
|
|
|
const url = this.getSigninURL(token, type);
|
|
|
|
|
|
|
|
const info = await this.transporter.sendMail({
|
|
|
|
to: options.email,
|
|
|
|
subject: this.getSubject(type),
|
|
|
|
text: this.getText(url, type, options.email),
|
|
|
|
html: this.getHTML(url, type, options.email)
|
|
|
|
});
|
|
|
|
|
|
|
|
return {token, info};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getMagicLink
|
|
|
|
*
|
|
|
|
* @param {object} options
|
2020-09-17 17:52:27 +03:00
|
|
|
* @param {TokenData} options.tokenData - The data for token
|
2020-09-17 17:42:01 +03:00
|
|
|
* @param {string=} [options.type='signin'] - The type to be passed to the url and content generator functions
|
|
|
|
* @returns {Promise<URL>} - signin URL
|
|
|
|
*/
|
|
|
|
async getMagicLink(options) {
|
2020-09-17 17:52:27 +03:00
|
|
|
const token = await this.tokenProvider.create(options.tokenData);
|
2020-09-17 17:42:01 +03:00
|
|
|
|
|
|
|
const type = options.type || 'signin';
|
|
|
|
|
|
|
|
return this.getSigninURL(token, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getDataFromToken
|
|
|
|
*
|
2020-09-17 17:52:27 +03:00
|
|
|
* @param {Token} token - The token to decode
|
|
|
|
* @returns {Promise<TokenData>} data - The data object associated with the magic link
|
2020-09-17 17:42:01 +03:00
|
|
|
*/
|
|
|
|
async getDataFromToken(token) {
|
2020-09-17 17:52:27 +03:00
|
|
|
const tokenData = await this.tokenProvider.validate(token);
|
2020-09-17 17:42:01 +03:00
|
|
|
return tokenData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 06:07:03 +03:00
|
|
|
/**
|
|
|
|
* defaultGetText
|
|
|
|
*
|
|
|
|
* @param {URL} url - The url which will trigger sign in flow
|
2019-10-01 09:51:43 +03:00
|
|
|
* @param {string} type - The type of email to send e.g. signin, signup
|
2019-10-10 16:19:31 +03:00
|
|
|
* @param {string} email - The recipient of the email to send
|
2019-09-03 06:07:03 +03:00
|
|
|
* @returns {string} text - The text content of an email to send
|
|
|
|
*/
|
2019-10-10 16:19:31 +03:00
|
|
|
function defaultGetText(url, type, email) {
|
2019-10-01 09:51:43 +03:00
|
|
|
let msg = 'sign in';
|
|
|
|
if (type === 'signup') {
|
|
|
|
msg = 'confirm your email address';
|
|
|
|
}
|
2019-10-10 16:19:31 +03:00
|
|
|
return `Click here to ${msg} ${url}. This msg was sent to ${email}`;
|
2019-09-03 06:07:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* defaultGetHTML
|
|
|
|
*
|
|
|
|
* @param {URL} url - The url which will trigger sign in flow
|
2019-10-01 09:51:43 +03:00
|
|
|
* @param {string} type - The type of email to send e.g. signin, signup
|
2019-10-10 16:19:31 +03:00
|
|
|
* @param {string} email - The recipient of the email to send
|
2019-09-03 06:07:03 +03:00
|
|
|
* @returns {string} HTML - The HTML content of an email to send
|
|
|
|
*/
|
2019-10-10 16:19:31 +03:00
|
|
|
function defaultGetHTML(url, type, email) {
|
2019-10-01 09:51:43 +03:00
|
|
|
let msg = 'sign in';
|
|
|
|
if (type === 'signup') {
|
|
|
|
msg = 'confirm your email address';
|
|
|
|
}
|
2019-10-10 16:19:31 +03:00
|
|
|
return `<a href="${url}">Click here to ${msg}</a> This msg was sent to ${email}`;
|
2019-09-03 06:07:03 +03:00
|
|
|
}
|
|
|
|
|
2019-10-10 16:19:56 +03:00
|
|
|
/**
|
|
|
|
* defaultGetSubject
|
|
|
|
*
|
|
|
|
* @param {string} type - The type of email to send e.g. signin, signup
|
|
|
|
* @returns {string} subject - The subject of an email to send
|
|
|
|
*/
|
|
|
|
function defaultGetSubject(type) {
|
|
|
|
if (type === 'signup') {
|
|
|
|
return `Signup!`;
|
|
|
|
}
|
|
|
|
return `Signin!`;
|
|
|
|
}
|
|
|
|
|
2020-09-17 17:42:01 +03:00
|
|
|
module.exports = MagicLink;
|