Ghost/core/server/auth/sync.js
Katharina Irrgang 319a388277 ghost auth: sync email (#8027)
*   ghost auth: sync email

refs #7452

- sync email changes in background (every hour right now)
- sync logged in user only!
- no sync if auth strategy password is used
- GET /users/me is triggered on every page refresh
- added TODO to support or add long polling for syncing data later
- no tests yet on purpose, as i would like to get a basic review first

* 🐩  use events

- remember sync per user
2017-02-23 18:04:24 +00:00

64 lines
1.9 KiB
JavaScript

var debug = require('ghost-ignition').debug('sync'),
models = require('../models'),
ghostAuth = require('./ghost-auth'),
logging = require('../logging'),
errors = require('../errors'),
events = require('../events'),
knex = require('../data/db').knex,
_private = {
syncIntervalInMs: 1000 * 60 * 60,
lastSync: {}
};
/**
* @TODO: support long polling in the ghost auth service
*/
_private.syncUser = function syncUser(loggedInUserModel) {
debug('syncUser');
// CASE: sync every hour for now
if (_private.lastSync[loggedInUserModel.id]) {
if ((_private.lastSync[loggedInUserModel.id] + _private.syncIntervalInMs) > Date.now()) {
debug('too early too sync');
return;
}
}
return ghostAuth.getUser({
token: loggedInUserModel.get('ghost_auth_access_token')
}).then(function (ghostUser) {
debug('ghost_email', ghostUser.email);
debug('user_email', loggedInUserModel.get('email'));
if (ghostUser.email === loggedInUserModel.get('email')) {
debug('email has not changed');
return;
}
debug('sync email');
// CASE: we update the user in a transaction to avoid collisions
return knex.transaction(function onTransaction(transaction) {
return models.User.edit({
email: ghostUser.email
}, {id: loggedInUserModel.id, transacting: transaction});
});
}).then(function () {
debug('update lastSync');
_private.lastSync[loggedInUserModel.id] = Date.now();
}).catch(function onError(err) {
logging.error(new errors.InternalServerError({
message: 'ghost-auth: sync failed',
err: err
}));
});
};
module.exports.init = function init(options) {
var authType = options.authType;
if (authType === 'ghost') {
events.on('read:users:me', _private.syncUser);
}
};