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

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

    I had the same issue by forgetting to add

    request.login()
    

    on

    app.post('/login', 
        function(request, response, next) {
            console.log(request.session)
            passport.authenticate('login', 
            function(err, user, info) {
                if(!user){ response.send(info.message);}
                else{
    
                    request.login(user, function(error) {
                        if (error) return next(error);
                        console.log("Request Login supossedly successful.");
                        return response.send('Login successful');
                    });
                    //response.send('Login successful');
                }
    
            })(request, response, next);
        }
    );
    

    Hopefully that might help for others that ended up here same reason as I did.

    0 讨论(0)
  • 2020-11-29 02:17

    I also was facing same problem, but @PVThomas gives me solution, as in here in Answers. My problem was with findById() method in deserialize(). I was using findOne() in findById() and then I replaced it with find() and now req.isAuthenticated() is working fine. My app wasn't saving req.session.passport.user, It was returning undefined and then after replacement of findOne() with find() it's saving user id in req.session.passport.user.

    0 讨论(0)
  • 2020-11-29 02:19

    app.use( session({ secret: 'Our little secret.', resave: false, saveUninitialized: true, cookie: { secure: true } << it was extra for me }) );

    0 讨论(0)
  • 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)
    0 讨论(0)
  • 2020-11-29 02:25

    If you wrap your routes like so:

    module.exports = function(){
    
    router.get('/',(req,res)=>{
     res.send('stuff');
      }
    
    }
    

    You have to pass "app and passport" to your routes like so:

    module.exports = function(app,passport){
    
    //routes n stuff
    
    }
    
    0 讨论(0)
  • 2020-11-29 02:26

    My problem was that i set cookie.secure to true even if data was not over https.

    app.use(require('express-session')({
        secret: process.env.sessionSecret,
        cookie: {
            maxAge: 1000 * 60 * 60 * 24 * 7 // 1 week
        },
        store: store,
        resave: false,
        saveUninitialized: false,
        cookie: { secure: false } // Remember to set this
    }));
    

    Remember to set cookies to false if you're not using https

    cookie: { secure: false } // Set to false
    

    Also if you do believe you have https remember to trust the proxy

    app.set('trust proxy', 1) // trust first proxy
    
    0 讨论(0)
提交回复
热议问题