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

后端 未结 7 1263
佛祖请我去吃肉
佛祖请我去吃肉 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:27

    In reference to the Passport documentation, the user object is contained in req.user. See below.

        app.post('/login',
          passport.authenticate('local'),function(req, res) {
           // If this function gets called, authentication was successful.
           // `req.user` contains the authenticated user.
           res.redirect('/users/' + req.user.username);
         });
    

    That way, you can access your user object from the page you redirect to.

    In case you get stuck, you can refer to my Github project where I implemented it clearly.

提交回复
热议问题