From 2a51a478fc1175dd947489aee90fafad04394c30 Mon Sep 17 00:00:00 2001 From: Naz Date: Thu, 6 Feb 2020 17:08:39 +0800 Subject: [PATCH] Exposed getMagicLink method (#123) refs https://github.com/TryGhost/Ghost/pull/11573 - Adds `getMagicLink` method to members-api which can be used to generate a signin link for the member --- ghost/magic-link/index.js | 19 +++++++++++++++++++ ghost/members-api/index.js | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/ghost/magic-link/index.js b/ghost/magic-link/index.js index a71cb2a55c..68321f8fd1 100644 --- a/ghost/magic-link/index.js +++ b/ghost/magic-link/index.js @@ -107,6 +107,25 @@ MagicLink.prototype.sendMagicLink = async function sendMagicLink(options) { return {token, info}; }; +/** + * getMagicLink + * + * @param {object} options + * @param {object} options.subject - The subject to associate with the magic link (user id, or email) + * @param {string=} [options.type='signin'] - The type to be passed to the url and content generator functions + * @returns {string} - signin URL + */ +MagicLink.prototype.getMagicLink = function getMagicLink(options) { + const token = jwt.sign({}, this.secret, { + algorithm: 'HS256', + subject: options.subject, + expiresIn: '10m' + }); + + const type = options.type || 'signin'; + + return this.getSigninURL(token, type); +}; /** * getUserFromToken diff --git a/ghost/members-api/index.js b/ghost/members-api/index.js index 9b32765905..375a3ae393 100644 --- a/ghost/members-api/index.js +++ b/ghost/members-api/index.js @@ -85,6 +85,10 @@ module.exports = function MembersApi({ } } + function getMagicLink(email) { + return magicLinkService.getMagicLink({email, subject: email, type: 'signin'}); + } + const users = Users({ stripe, memberModel @@ -329,6 +333,7 @@ module.exports = function MembersApi({ getPublicConfig, bus, sendEmailWithMagicLink, + getMagicLink, members: users }; };