Skip to content

Commit

Permalink
Merge pull request strongloop#3160 from strongloop/fix/token-invalida…
Browse files Browse the repository at this point in the history
…tion-on-save

Preserve sessions on User.save() making no changes
  • Loading branch information
bajtos authored Jan 31, 2017
2 parents c7e0a15 + 8f80aec commit dcb2f15
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
27 changes: 9 additions & 18 deletions common/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,34 +863,24 @@ module.exports = function(User) {
next();
});

// Delete old sessions once email is updated
User.observe('before save', function beforeEmailUpdate(ctx, next) {
User.observe('before save', function prepareForTokenInvalidation(ctx, next) {
if (ctx.isNewInstance) return next();
if (!ctx.where && !ctx.instance) return next();

var pkName = ctx.Model.definition.idName() || 'id';
var where = ctx.where;
if (!where) {
where = {};
where[pkName] = ctx.instance[pkName];
}

var isPartialUpdateChangingPassword = ctx.data && 'password' in ctx.data;

// Full replace of User instance => assume password change.
// HashPassword returns a different value for each invocation,
// therefore we cannot tell whether ctx.instance.password is the same
// or not.
var isFullReplaceChangingPassword = !!ctx.instance;

ctx.hookState.isPasswordChange = isPartialUpdateChangingPassword ||
isFullReplaceChangingPassword;

ctx.Model.find({where: where}, function(err, userInstances) {
if (err) return next(err);
ctx.hookState.originalUserData = userInstances.map(function(u) {
var user = {};
user[pkName] = u[pkName];
user['email'] = u['email'];
user.email = u.email;
user.password = u.password;
return user;
});
var emailChanged;
Expand All @@ -912,18 +902,19 @@ module.exports = function(User) {
});
});

User.observe('after save', function afterEmailUpdate(ctx, next) {
User.observe('after save', function invalidateOtherTokens(ctx, next) {
if (!ctx.instance && !ctx.data) return next();
if (!ctx.hookState.originalUserData) return next();

var pkName = ctx.Model.definition.idName() || 'id';
var newEmail = (ctx.instance || ctx.data).email;
var isPasswordChange = ctx.hookState.isPasswordChange;
var newPassword = (ctx.instance || ctx.data).password;

if (!newEmail && !isPasswordChange) return next();
if (!newEmail && !newPassword) return next();

var userIdsToExpire = ctx.hookState.originalUserData.filter(function(u) {
return (newEmail && u.email !== newEmail) || isPasswordChange;
return (newEmail && u.email !== newEmail) ||
(newPassword && u.password !== newPassword);
}).map(function(u) {
return u[pkName];
});
Expand Down
7 changes: 7 additions & 0 deletions test/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2107,6 +2107,13 @@ describe('User', function() {
});
});

it('keeps sessions AS IS when calling save() with no changes', function(done) {
user.save(function(err) {
if (err) return done(err);
assertPreservedTokens(done);
});
});

it('keeps sessions AS IS if firstName is added using `updateOrCreate`', function(done) {
User.updateOrCreate({
pk: user.pk,
Expand Down

0 comments on commit dcb2f15

Please sign in to comment.