0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-25 02:31:59 -05:00

Added type param to content generation functions

no-issue

This will allow conditional logic based on the type passed to sending the magic-link
This commit is contained in:
Fabien O'Carroll 2019-10-01 13:51:43 +07:00
parent 5170c7c1d4
commit d2634c7c7b
2 changed files with 25 additions and 11 deletions

View file

@ -14,20 +14,30 @@ module.exports = MagicLink;
* defaultGetText * defaultGetText
* *
* @param {URL} url - The url which will trigger sign in flow * @param {URL} url - The url which will trigger sign in flow
* @param {string} type - The type of email to send e.g. signin, signup
* @returns {string} text - The text content of an email to send * @returns {string} text - The text content of an email to send
*/ */
function defaultGetText(url) { function defaultGetText(url, type) {
return `Click here to sign in ${url}`; let msg = 'sign in';
if (type === 'signup') {
msg = 'confirm your email address';
}
return `Click here to ${msg} ${url}`;
} }
/** /**
* defaultGetHTML * defaultGetHTML
* *
* @param {URL} url - The url which will trigger sign in flow * @param {URL} url - The url which will trigger sign in flow
* @param {string} type - The type of email to send e.g. signin, signup
* @returns {string} HTML - The HTML content of an email to send * @returns {string} HTML - The HTML content of an email to send
*/ */
function defaultGetHTML(url) { function defaultGetHTML(url, type) {
return `<a href="${url}">Click here to sign in</a>`; let msg = 'sign in';
if (type === 'signup') {
msg = 'confirm your email address';
}
return `<a href="${url}">Click here to ${msg}</a>`;
} }
/** /**
@ -38,7 +48,7 @@ function defaultGetHTML(url) {
* @param {MailTransporter} options.transporter * @param {MailTransporter} options.transporter
* @param {RsaPublicKey} options.publicKey * @param {RsaPublicKey} options.publicKey
* @param {RsaPrivateKey} options.privateKey * @param {RsaPrivateKey} options.privateKey
* @param {(token: JSONWebToken) => URL} options.getSigninURL * @param {(token: JSONWebToken, type: string) => URL} options.getSigninURL
* @param {typeof defaultGetText} [options.getText] * @param {typeof defaultGetText} [options.getText]
* @param {typeof defaultGetHTML} [options.getHTML] * @param {typeof defaultGetHTML} [options.getHTML]
*/ */
@ -60,6 +70,7 @@ function MagicLink(options) {
* @param {object} options * @param {object} options
* @param {string} options.email - The email to send magic link to * @param {string} options.email - The email to send magic link to
* @param {object} options.user - The user object to associate with the magic link * @param {object} options.user - The user object to associate with the magic link
* @param {string=} [options.type='signin'] - The type to be passed to the url and content generator functions
* @returns {Promise<{token: JSONWebToken, info: SentMessageInfo}>} * @returns {Promise<{token: JSONWebToken, info: SentMessageInfo}>}
*/ */
MagicLink.prototype.sendMagicLink = async function sendMagicLink(options) { MagicLink.prototype.sendMagicLink = async function sendMagicLink(options) {
@ -73,12 +84,14 @@ MagicLink.prototype.sendMagicLink = async function sendMagicLink(options) {
expiresIn: '10m' expiresIn: '10m'
}); });
const url = this.getSigninURL(token); const type = options.type || 'signin';
const url = this.getSigninURL(token, type);
const info = await this.transporter.sendMail({ const info = await this.transporter.sendMail({
to: options.email, to: options.email,
text: this.getText(url), text: this.getText(url, type),
html: this.getHTML(url) html: this.getHTML(url, type)
}); });
return {token, info}; return {token, info};

View file

@ -22,7 +22,7 @@ describe('MagicLink', function () {
}); });
describe('#sendMagicLink', function () { describe('#sendMagicLink', function () {
it('Sends an email to the user with a link generated from getSigninURL(token)', async function () { it('Sends an email to the user with a link generated from getSigninURL(token, type)', async function () {
const options = { const options = {
publicKey, publicKey,
privateKey, privateKey,
@ -39,12 +39,13 @@ describe('MagicLink', function () {
email: 'test@example.com', email: 'test@example.com',
user: { user: {
id: 420 id: 420
} },
type: 'blazeit'
}; };
const {token} = await service.sendMagicLink(args); const {token} = await service.sendMagicLink(args);
should.ok(options.getSigninURL.calledOnce); should.ok(options.getSigninURL.calledOnce);
should.ok(options.getSigninURL.firstCall.calledWithExactly(token)); should.ok(options.getSigninURL.firstCall.calledWithExactly(token, 'blazeit'));
should.ok(options.transporter.sendMail.calledOnce); should.ok(options.transporter.sendMail.calledOnce);
should.equal(options.transporter.sendMail.firstCall.args[0].to, args.email); should.equal(options.transporter.sendMail.firstCall.args[0].to, args.email);