护照的 req.isAuthenticated 总是返回 false,即使我硬编码完成(null,true)

2023-11-29

我正在努力让我的护照本地策略发挥作用。

我已经设置了这个中间件:

passport.use(new LocalStrategy(function(username, password, done) {
    //return done(null, user);
    if (username=='ben' && password=='benny'){
        console.log("Password correct");
        return done(null, true);
    }
    else
        return done(null, false, {message: "Incorrect Login"});
}));

但然后在这里

app.use('/admin', adminIsLoggedIn, admin);

function adminIsLoggedIn(req, res, next) {

    // if user is authenticated in the session, carry on 
    if (req.isAuthenticated())
        return next();

    // if they aren't redirect them to the home page
    res.redirect('/');
}

它总是失败并重定向到主页。

我不明白为什么会发生这种情况?为什么验证不了呢?

在我的控制台中我可以看到Password Correct正在打印。 为什么行不通?


我有类似的问题。可能是由于护照所需的快速会话中间件。按以下顺序使用中间件修复了该问题:(Express 4)

var session = require('express-session');

// required for passport session
app.use(session({
  secret: 'secrettexthere',
  saveUninitialized: true,
  resave: true,
  // using store session on MongoDB using express-session + connect
  store: new MongoStore({
    url: config.urlMongo,
    collection: 'sessions'
  })
}));

// Init passport authentication 
app.use(passport.initialize());
// persistent login sessions 
app.use(passport.session());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

护照的 req.isAuthenticated 总是返回 false,即使我硬编码完成(null,true) 的相关文章

随机推荐