Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions common/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ module.exports = function(User) {
};

User.observe('before delete', function(ctx, next) {
// Do nothing when the access control was disabled for this user model.
if (!ctx.Model.relations.accessTokens) return next();

var AccessToken = ctx.Model.relations.accessTokens.modelTo;
var pkName = ctx.Model.definition.idName() || 'id';
ctx.Model.find({where: ctx.where, fields: [pkName]}, function(err, list) {
Expand Down
17 changes: 17 additions & 0 deletions test/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,23 @@ describe('User', function() {
});
});

it('skips token invalidation when the relation is not configured', () => {
const app = loopback({localRegistry: true, loadBuiltinModels: true});
app.dataSource('db', {connector: 'memory'});

const PrivateUser = app.registry.createModel({
name: 'PrivateUser',
base: 'User',
// Speed up the password hashing algorithm for tests
saltWorkFactor: 4,
});
app.model(PrivateUser, {dataSource: 'db'});

return PrivateUser.create({email: 'private@example.com', password: 'pass'})
.then(u => PrivateUser.deleteById(u.id));
// the test passed when the operation did not crash
});

it('invalidates the user\'s accessToken when the user is deleted all', function(done) {
var userIds = [];
var users;
Expand Down