Loopback : How to get the current user on loopback models

To get the current user add following code on server.js :

app.use(function (req, res, next) {
  if (!req.accessToken) return next();
  var userId = req.accessToken.userId;
  app.models.User.findById(userId, function(err, user) {
    if (err) return next(err);
    if (!user) return next('No user found!');
    var loopbackContext = loopback.getCurrentContext();
    if (loopbackContext) 
       loopbackContext.set('currentUser', user);
    next();
  });
});

To get current user from model :

var loopback = require('loopback');
...
....
var context = loopback.getCurrentContext();
var user = context && context.get("currentUser");
Advertisements

2 thoughts on “Loopback : How to get the current user on loopback models

  1. Thank you — this really helped me. I am still blown away by the lack of any real documentation on loopback, so this was extremely helpful. Since my code that needed to do this is adding log output for each loopback API call to show who made it (i.e., audit log) I can’t do the lookup every time (obviously would be too expensive), so I built a little in-memory cache so it looks up through the cache, mapping user IDs to user names as long as the server is running. So it does the lookup just once per user per run of the server, thus it really screams. I wish (and still wonder if) there was a way to get the username without having to do the lookup (I am still confused what loopback “context” is (beyond the obvious) and how I can use it myself, since I would presume it already did that so if I hooked my audit log code in as middleware i.e. at the “session” stage, I would expect maybe they actually had that information in req already. But I honestly am just completely overwhelmed by the lack of documentation. Sure, I can read the code, but it’s not ideal to have to do so, and it’s often hard to uncover the most important points w/o someone coming up with an example such as you’ve done. So, again, thanks very much.

    1. Yes, lookup on every request is not an good idea. You can update the code like this:

      app.use(function (req, res, next) {
          if (!req.accessToken) return next();
          var loopbackContext = loopback.getCurrentContext();
          var user = userManager.users[req.accessToken.userId];
          if(user){
            if (loopbackContext) loopbackContext.set('currentUser', user);
            next();
          }else{
            app.models.User.findById(req.accessToken.userId, function(err, user) {
              if (err) return next(err);
              if (!user) return next('Invalid AccessToken!');
              userManager.users[user.id] = user
              if (loopbackContext) loopbackContext.set('currentUser', user);
              next();
            });
          }
          
      });

      Here userManager.users : the cache users

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s