0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00

Added labels to member signup flow (#124)

no issue

refs https://github.com/TryGhost/Ghost/pull/11538
This commit is contained in:
Rishabh Garg 2020-02-12 16:42:49 +05:30 committed by GitHub
parent 5e2256833c
commit 789462aa5f
3 changed files with 46 additions and 11 deletions

View file

@ -83,12 +83,14 @@ function MagicLink(options) {
*
* @param {object} options
* @param {string} options.email - The email to send magic link to
* @param {string} options.payload - The payload for token
* @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 {Promise<{token: JSONWebToken, info: SentMessageInfo}>}
*/
MagicLink.prototype.sendMagicLink = async function sendMagicLink(options) {
const token = jwt.sign({}, this.secret, {
const payload = options.payload || {};
const token = jwt.sign(payload, this.secret, {
algorithm: 'HS256',
subject: options.subject,
expiresIn: '10m'
@ -141,3 +143,18 @@ MagicLink.prototype.getUserFromToken = function getUserFromToken(token) {
});
return claims.sub;
};
/**
* getPayloadFromToken
*
* @param {JSONWebToken} token - The token to decode
* @returns {object} payload - The payload object associated with the magic link
*/
MagicLink.prototype.getPayloadFromToken = function getPayloadFromToken(token) {
/** @type {object} */
const claims = jwt.verify(token, this.secret, {
algorithms: ['HS256'],
maxAge: '10m'
});
return claims || {};
};

View file

@ -72,16 +72,16 @@ module.exports = function MembersApi({
getSubject
});
async function sendEmailWithMagicLink(email, requestedType, options = {forceEmailType: false}){
async function sendEmailWithMagicLink({email, requestedType, payload, options = {forceEmailType: false}}){
if (options.forceEmailType) {
return magicLinkService.sendMagicLink({email, subject: email, type: requestedType});
return magicLinkService.sendMagicLink({email, payload, subject: email, type: requestedType});
}
const member = await users.get({email});
if (member) {
return magicLinkService.sendMagicLink({email, subject: email, type: 'signin'});
return magicLinkService.sendMagicLink({email, payload, subject: email, type: 'signin'});
} else {
const type = requestedType === 'subscribe' ? 'subscribe' : 'signup';
return magicLinkService.sendMagicLink({email, subject: email, type});
return magicLinkService.sendMagicLink({email, payload, subject: email, type});
}
}
@ -96,14 +96,16 @@ module.exports = function MembersApi({
async function getMemberDataFromMagicLinkToken(token){
const email = await magicLinkService.getUserFromToken(token);
const {labels = []} = await magicLinkService.getPayloadFromToken(token);
if (!email) {
return null;
}
const member = await getMemberIdentityData(email);
if (member) {
return member;
}
await users.create({email});
await users.create({email, labels});
return getMemberIdentityData(email);
}
async function getMemberIdentityData(email){
@ -131,14 +133,18 @@ module.exports = function MembersApi({
return res.end('Bad Request.');
}
const emailType = req.body.emailType;
const payload = {};
if (req.body.labels) {
payload.labels = req.body.labels;
}
try {
if (!allowSelfSignup) {
const member = await users.get({email});
if (member) {
await sendEmailWithMagicLink(email, emailType);
await sendEmailWithMagicLink({email, requestedType: emailType});
}
} else {
await sendEmailWithMagicLink(email, emailType);
await sendEmailWithMagicLink({email, requestedType: emailType, payload});
}
res.writeHead(201);
return res.end('Created.');
@ -238,7 +244,7 @@ module.exports = function MembersApi({
}
const emailType = 'signup';
await sendEmailWithMagicLink(customer.email, emailType, {forceEmailType: true});
await sendEmailWithMagicLink({email: customer.email, requestedType: emailType, options: {forceEmailType: true}});
}
res.writeHead(200);

View file

@ -1,13 +1,15 @@
const _ = require('lodash');
const debug = require('ghost-ignition').debug('users');
const common = require('./common');
let Member;
async function createMember({email, name, note}) {
async function createMember({email, name, note, labels}) {
const model = await Member.add({
email,
name,
note
note,
labels
});
const member = model.toJSON();
return member;
@ -153,6 +155,16 @@ module.exports = function ({
async function create(data) {
debug(`create email:${data.email}`);
/** Member.add model method expects label object array*/
if (data.labels) {
data.labels.forEach((label, index) => {
if (_.isString(label)) {
data.labels[index] = {name: label};
}
});
}
const member = await createMember(data);
return member;
}