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

前端 未结 14 1412
小鲜肉
小鲜肉 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条回答
  •  萌比男神i
    2020-11-29 02:30

    Resolved in my case, I also faced the same problem, but resolved just by reordering the code as mentioned below:

    //--------------------------------

    previous code :

    app.use(flash())
    app.use(session({
        secret: 'somesecret',
        resave: false,
        saveUninitialized: false
    }))
    // using the custom middleware for storing variable in response
    app.use((req, res, next) => {
        res.locals.isAuthenticated = req.isAuthenticated()
        next()
    })
    app.use(passport.initialize())
    app.use(passport.session())
    

    //--------------------------------

    Refactored code : (which fixed the problem):

    app.use(flash())
    app.use(session({
        secret: 'somesecret',
        resave: false,
        saveUninitialized: false
    }))
    app.use(passport.initialize())
    app.use(passport.session())
    
    // using the custom middleware for storing variable in response
    app.use((req, res, next) => {
        res.locals.isAuthenticated = req.isAuthenticated()
        next()
    })
    

    //--------------------------------

提交回复
热议问题