1

I am new to passport and sails.js. I created an user login follow by this article. It's work fine and I can do both login and logout.

However, I try to access current login user in some Controller but I cannot get the user data.

I have reading some topic here and it said something likes this

test : function(req,res){
    console.log(req.user);
}

I try this with my TestController.js

module.exports = {
    test : function(req,res){
        console.log(req.user);
        res.send("test");
        res.status(200).end();
    }
};

I got undefined.

Natsathorn
  • 1,530
  • 1
  • 12
  • 26

1 Answers1

0

I have fixing this problem Thanks to @godfather

Before you can access to user object from request you have to deserialize it first.

passport.deserializeUser(function (id, done) {
    User.findOne({
        id: id 
    }, function (err, user) {
        delete user.password
        done(err, user);
    });
});

This deserializeUser function will attach your user object to request object. So, this will make you can access to current login user from other Controller.

More information here : Understanding passport serialize deserialize

Community
  • 1
  • 1
Natsathorn
  • 1,530
  • 1
  • 12
  • 26