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

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

    You can define your route this way as follows.

    router.post('/login',
    passport.authenticate('local' , {failureRedirect:'/login', failureFlash: true}),
    function(req, res) {
       res.redirect('/home?' + req.user.username);
    });
    

    In the above code snippet, you can access and pass any field of the user object as "req.user.field_name" to the page you want to redirect. One thing to note here is that the base url of the page you want to redirect to should be followed by a question mark.

提交回复
热议问题