I think you should add a pre-processing middleware to populate the context with the current user.
/server/server.js
app.use(loopback.context());
app.use(loopback.token());
app.use(function setCurrentUser(req, res, next) {
if (!req.accessToken) {
return next();
}
app.models.Customer.findById(req.accessToken.userId, function(err, user) {
if (err) {
return next(err);
}
if (!user) {
return next(new Error('No user with this access token was found.'));
}
var loopbackContext = loopback.getCurrentContext();
if (loopbackContext) {
loopbackContext.set('currentUser', user);
}
next();
});
});
/common/models/customer.js
var loopback = require('loopback');
module.exports = function(Customer) {
Customer.observe('before save', function checkPermission(ctx, next) {
var context = loopback.getCurrentContext();
var currentUser = context && context.get('currentUser');
console.log(currentUser);
next();
});
};