mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Added members service to create instance of members lib (#10261)
refs #10213 * Added members service to create instance of members lib
This commit is contained in:
parent
a5ea34900b
commit
0b58f4cf62
2 changed files with 120 additions and 0 deletions
111
core/server/services/members/api.js
Normal file
111
core/server/services/members/api.js
Normal file
|
@ -0,0 +1,111 @@
|
|||
const settingsCache = require('../settings/cache');
|
||||
const config = require('../../config');
|
||||
const MembersApi = require('../../lib/members');
|
||||
const models = require('../../models');
|
||||
const URL = require('url').URL;
|
||||
const mail = require('../mail');
|
||||
|
||||
function createMember({name, email, password}) {
|
||||
return models.Member.add({
|
||||
name,
|
||||
email,
|
||||
password
|
||||
}).then((member) => {
|
||||
return member.toJSON();
|
||||
});
|
||||
}
|
||||
|
||||
function updateMember(member, newData) {
|
||||
return models.Member.findOne(member, {
|
||||
require: true
|
||||
}).then(({id}) => {
|
||||
return models.Member.edit(newData, {id});
|
||||
}).then((member) => {
|
||||
return member.toJSON();
|
||||
});
|
||||
}
|
||||
|
||||
function getMember(member) {
|
||||
return models.Member.findOne(member, {
|
||||
require: true
|
||||
}).then((member) => {
|
||||
return member.toJSON();
|
||||
});
|
||||
}
|
||||
|
||||
function validateMember({email, password}) {
|
||||
return models.Member.findOne({email}, {
|
||||
require: true
|
||||
}).then((member) => {
|
||||
return member.comparePassword(password).then((res) => {
|
||||
if (!res) {
|
||||
throw new Error('Password is incorrect');
|
||||
}
|
||||
return member;
|
||||
});
|
||||
}).then((member) => {
|
||||
return member.toJSON();
|
||||
});
|
||||
}
|
||||
|
||||
// @TODO this should check some config/settings and return Promise.reject by default
|
||||
function validateAudience({audience, origin}) {
|
||||
if (audience === origin) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
const publicKey = settingsCache.get('members_public_key');
|
||||
const privateKey = settingsCache.get('members_private_key');
|
||||
const sessionSecret = settingsCache.get('members_session_secret');
|
||||
const passwordResetUrl = config.get('url');
|
||||
const issuer = config.get('url');
|
||||
const ssoOrigin = new URL(config.get('url')).origin;
|
||||
let mailer;
|
||||
|
||||
function sendEmail(member, {token}) {
|
||||
if (!(mailer instanceof mail.GhostMailer)) {
|
||||
mailer = new mail.GhostMailer();
|
||||
}
|
||||
const message = {
|
||||
to: member.email,
|
||||
subject: 'Reset password',
|
||||
html: `
|
||||
Hi ${member.name},
|
||||
|
||||
To reset your password, click the following link and follow the instructions:
|
||||
|
||||
${passwordResetUrl}#reset-password?token=${token}
|
||||
|
||||
If you didn't request a password change, just ignore this email.
|
||||
`
|
||||
};
|
||||
|
||||
/* eslint-disable */
|
||||
// @TODO remove this
|
||||
console.log(message.html);
|
||||
/* eslint-enable */
|
||||
return mailer.send(message).catch((err) => {
|
||||
return Promise.reject(err);
|
||||
});
|
||||
}
|
||||
|
||||
const api = MembersApi({
|
||||
config: {
|
||||
issuer,
|
||||
publicKey,
|
||||
privateKey,
|
||||
sessionSecret,
|
||||
ssoOrigin
|
||||
},
|
||||
validateAudience,
|
||||
createMember,
|
||||
getMember,
|
||||
validateMember,
|
||||
updateMember,
|
||||
sendEmail
|
||||
});
|
||||
|
||||
module.exports = api;
|
||||
module.exports.publicKey = publicKey;
|
9
core/server/services/members/index.js
Normal file
9
core/server/services/members/index.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
const labs = require('../labs');
|
||||
module.exports = {
|
||||
get api() {
|
||||
if (!labs.isSet('members')) {
|
||||
return {};
|
||||
}
|
||||
return require('./api');
|
||||
}
|
||||
};
|
Loading…
Reference in a new issue