3

I want to get get current logged in user in a models afterCreate callback. In controller, current logged in user is found form the request header-req.user. But in model's life-cycle callback the session header is not passed. Is there any way to find the current user from model?

I want something like this:

afterCreate: function(record,next){
    var currentUser=this.req.user.id; //want to get the request header here with logged in user
    AnotherModel.create({something:record.something, createdBy:currentUser}).exec(function(err, anotherRecord){
        next()
    }); 
}
hasan3050
  • 195
  • 2
  • 10

1 Answers1

3

The req object is not available within the model (for reasons I will not go into).

But your issue is a common one. What many people have done is create a policy that will set that param automatically on the params being passed to the model.

Here are a bunch of related questions and answers

sails.js Use session param in model

Is it possible to access a session variable directly in a Model in SailsJS

https://github.com/balderdashy/waterline/issues/556

https://github.com/balderdashy/waterline/pull/787

Sails Google Group Discussion on the topic

Community
  • 1
  • 1
Meeker
  • 5,979
  • 2
  • 20
  • 38