Passport.js: how to access user object after authentication?

后端 未结 7 1256
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 14:01

I\'m using Passport.js to login a user with username and password. I\'m essentially using the sample code from the Passport site. Here are the relevant parts (I think) of my c

7条回答
  •  日久生厌
    2021-02-01 14:44

    Late to the party but found this unanswered after googling the answer myself.

    Inside the request will be a req.user object that you can work withr.

    Routes like so:

    app.get('/api/portfolio', passport.authenticate('jwt', { session: false }), stocks.buy);
    

    Controller like this:

    buy: function(req, res) {
          console.log(req.body);
          //res.json({lel: req.user._id});
          res.json({lel: req.user});
        }
    

提交回复
热议问题