Passport and Passport Local req.isAuthenticated always returns false

前端 未结 2 1441
终归单人心
终归单人心 2020-12-31 23:34

I haven\'t been able to track this down, but for my set up, isAuthenticated always returns false even after a successful login. Here\'s the passport code:

相关标签:
2条回答
  • 2021-01-01 00:08

    I guess you forgot to put: req.login(...) inside passport.authenticate('local', function(...){}).

    See here (at the end of the page)

    0 讨论(0)
  • 2021-01-01 00:12

    Apologies if my original question is not that useful in the first place, but...

    I found that my combination of passport, passport-local, and passport-local-mongoose, a solution was to simply create an invalidation method on my mongoose Schema (that has the passportLocalMongoose "plugged in", and when my /logout route gets hit I essentially remove that user's token. Here's that method:

    Account.statics.invalidateUserToken = function(email, cb) {
        var self = this;
        this.findOne({email: email}, function(err, usr) {
            if(err || !usr) {
                console.log('err');
            }
            usr.token = null;
            usr.save(function(err, usr) {
                if (err) {
                    cb(err, null);
                } else {
                    cb(false, 'removed');
                }
            });
        });
    };
    

    I presume it's more interesting to see this in context so again please feel free to refer to the repo listed in question...hope this helps someone.

    Also, if a core from one of the aformentioned libs wants to suggest a better way I'd of course love to refactor my code to make it idiomatic; if not, this approach seemed to work.

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