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

前端 未结 14 1446
小鲜肉
小鲜肉 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:34

    I fixed this issue by fixing my passport.deserializeUser. I'm using mongo native and since most of the examples use Mongoose i fell in to the _id trap once again.

    So remember to make the _id a mongo ObjectID when reading the user in deserializeUser

    passport.deserializeUser(function(user, done) {
        const collection = db.get().collection('users')
        const userId = new mongo.ObjectID(user);
        collection.findOne({_id : userId}, function(err, user) {
            if (err) done(err, null);
            done(null, user);
        });
    });
    

    My query was not finding the user since I did not make the id an ObjectID, and there was no errors indicated anywhere.

提交回复
热议问题