最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

护照的req.isAuthenticated总是返回假的,甚至当我做硬编码(空,真)

IT培训 admin 8浏览 0评论

护照的req.isAuthenticated总是返回假的,甚至当我做硬编码(空,真)

我试图让我的护照地方战略的工作。

我有这个中间件设置:

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正在打印。为什么不工作?

回答如下:

我有一个类似的问题。可能是由于需要办理护照快递会话中间件。通过以如下顺序使用中间件固定它:(快速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());

护照的req.isAuthenticated总是返回假的,甚至当我做硬编码(空,真)

我试图让我的护照地方战略的工作。

我有这个中间件设置:

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正在打印。为什么不工作?

回答如下:

我有一个类似的问题。可能是由于需要办理护照快递会话中间件。通过以如下顺序使用中间件固定它:(快速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());
发布评论

评论列表 (0)

  1. 暂无评论