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

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

    This could also be an issue with your client's POST/GET calls. I had this exact same issue but it turned out that I had to give fetch (which is what I was using) the option credentials:'include' like so:

    fetch('/...', {
      method: 'POST',
      headers: myHeaders,
      credentials: 'include',
      body: ...
      ...})
    

    The reason is because fetch doesn't support passing down cookies, which is necessary in this case.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题