passport's req.isAuthenticated always returning false, even when I hardcode done(null, true)

前端 未结 14 1444
小鲜肉
小鲜肉 2020-11-29 02:06

I\'m trying to get my Passport local strategy working.

I\'ve got this middleware set up:

passport.use(new LocalStrategy(function(username, password,         


        
14条回答
  •  盖世英雄少女心
    2020-11-29 02:25

    FOR NEWBIES

    I was facing a similar problem, where my isAuthenticated() function would return false.I lost a lot of time, hope this answer saves yours.

    Some Common problems to watch out for,

    1. Middleware setup order (express-session > pass.initialize > pass.session ).
    2. Serialize and Deserialize methods needs to pass user on the request.(For more info I've posted an answer on this link.. Basics of Passport Session (expressjs)-why do we need to serialize and deserialize? ) if there's no user on request then isAuthenticated would return false.... and redirect to the PATH defined ......when false....
    3. The getUserById or findById function defined in the model(user.js) needs to have a User.findById (and not User.findOne) function defined.(this function would load user on the request in every session)

提交回复
热议问题