Passport req.isAuthenticated () always returns fasle

I am trying to use express session to login. I would like users to be able to go to the profile page and view their user data if they are logged in.

On line 9 of my routes .js: req.isAuthenticate () returns false, even if I have already successfully signed up with the user.

My ultimate goal is for req.user, this passport is saved in a session that needs to be defined in the GET / passport route so that I can send it as data to my ratchet mechanism.

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

Thank you in advance

+4
source share
1 answer

I believe something is wrong with your serialization / deserialization.

When serializing, you need to specify how you are going to serialize the user. According to your code, you use user IDs to serialize them. And this is what will be used for deserialization. Thus, you should expect only the identifier from the input parameters, and you need to find the user associated with this identifier and pass it to the callback.

This will 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)
  })
})
+3
source

Source: https://habr.com/ru/post/1621922/


All Articles