3

I'm using the built in user model of loopback for my application. What I want to accomplish is, that the author of a specific model entry (an article for example) is automatically saved to the model identified by the access token when it's saved.

I tried to implement this, by using the relationship manager, but the built in user model is not listed there.

Optimally when querying all articles, I would like the username to be available to show it in an overview but only if the current user is authenticated.

** Update **

After quite some research I found at least a way to add the current user to the loopback-context:

// see https://docs.strongloop.com/display/public/LB/Using+current+context
app.use(loopback.context());
app.use(loopback.token());
app.use(function setCurrentUser(req, res, next) {
  console.log(req.accessToken);
  if (!req.accessToken) {
    return next();
  }
  app.models.user.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();
  });
});

Now I'm trying to add the user via a mixin:

module.exports = function (Model) {
  Model.observe('before save', function event(ctx, next) {
    var user;
    var loopbackContext = loopback.getCurrentContext();
    if (loopbackContext && loopbackContext.active && loopbackContext.active.currentUser) {
      user = loopbackContext.active.currentUser;
      console.log(user);
      if (ctx.instance) {
        ctx.instance.userId = user.id;
      } else {
        ctx.data.userId = user.id;
      }
    }
    next();
  });
};

I also opened an issue on github.

Daniel Higueras
  • 2,404
  • 22
  • 34
hugo der hungrige
  • 12,382
  • 9
  • 57
  • 84
  • I'm using exactly the same approach and it works for me (means, user's ID is saved under userId property for each model uses this mixin). Did you add the mixin to your model? – Reuven Chacha Feb 12 '16 at 01:37
  • I did. Seems like I just had to clear my database one more time. Now it works! :) – hugo der hungrige Feb 12 '16 at 18:39

2 Answers2

4

I know this question is old but this might help some people

In LoopBack 2.x, you need to add this to your model JSON

"injectOptionsFromRemoteContext": true

then:

Model.observe('before save', function(ctx, next) {
  if(ctx.instance){
     ctx.instance.userId = ctx.options.accessToken.userId;
  }else if (ctx.data){
     ctx.data.userId = ctx.options.accessToken.userId;
  }
  return next();
});
b3rew
  • 340
  • 2
  • 9
-2

I strongly suggest you to extend the builtin User model even if the you does not need to create any new property. It's easy to extend using the arc console or creating the file manually and the entire process of authentication/authorization will work normally without any other additional configuration. Take a look here: https://docs.strongloop.com/display/public/LB/Extending+built-in+models

But even so if you want to use the builtin model User make sure that it is persisted in the database to show in the relationships manager list https://docs.strongloop.com/display/public/LB/Creating+database+tables+for+built-in+models

nsbm
  • 5,842
  • 6
  • 30
  • 45
  • This doesn't really answer the question which is attaching a relationship on save. – Jordan Kasper Feb 09 '16 at 15:54
  • @jakarella : you could very well extend User model and simply add a `MyUser` `hasMany` `Article` relation. This is just as valid, less code to write and leverages the whole querying system. With your approach, if for instance you want to fetch some data from the user, like its name for instance, you will need to implement more code, for each `GET api\Articles`, to find the user. – Overdrivr Feb 15 '16 at 08:29