简体   繁体   中英

passport req.isAuthenticated() always returns fasle

I'm trying to use express session to login users. I'd like users to be able to go to a profile page and view their user data if they have logged in.

On line 9 of my routes.js: req.isAuthenticate() is returning false even though I've already successfully signed in a user.

My ultimate goal is for req.user, that passport saves to the session, to be defined in the /passport GET route so I can send it as data to my templeting engine.

https://gist.github.com/anonymous/f6ecb472eb082775181e#file-routes-js-L9

Thanks in advance

I believe there is something wrong with your serialization/deserialization.

On serialization, you need to specify how you are going to serialize the user. According to your code you are using the users' ids to serialize them. And this is what is going to be used for deserialization. So, you should expect just an ID from the input parameters, and you have to find the user linked to that ID and pass it to the callback.

This would be what you should do:

passport.serializeUser(function(user, done){
  done(null, user.id)
})

passport.deserializeUser(function(id, done){
  User.findById(id, function(err, user){
    done(err, user)
  })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM