【问题标题】:User is not authenticated after res.redirect()res.redirect() 后用户未通过身份验证
【发布时间】:2015-01-09 06:11:44
【问题描述】:

我遇到了一个问题,尽管登录成功(如successRedirect 处理程序触发所证明),请求仍未通过身份验证,因此我的身份验证中间件再次将我们发送回登录页面。

我的路线如下:

// ROUTES

module.exports = function(app, passport) {

app.get('/home', isLoggedIn, function(req, res) {
  res.sendfile('imconfusedindex.html'); // this never gets sent.
});

app.post('/login', passport.authenticate('ldap-auth', {
    successRedirect: '/home',
    failureRedirect: '/login'
}));

}

// route middleware to make sure a user is logged in
function isLoggedIn(req, res, next) {

  // if user is authenticated, we'll all float on OK
  if (req.isAuthenticated()) {
    return next();
  }
  // otherwise, redirect them to the login page
  res.redirect('/login');
}

我的护照配置如下:

passport.serializeUser(function(user, done) {
  done(null, user.user_id);
});

passport.deserializeUser(function(id, done) {
  connection.query("select * from users where user_id = " + id, function(err, rows) {
    done(err, rows[0]);
  });
});

passport.use('ldap-auth', new LocalStrategy(
  function(username, password, done) {
    done(null, {user_id: 2, username: 'bob'});
  })
);

正如您在护照配置中看到的那样,我每次都返回一个虚拟用户。再做调试,发现请求正在被认证,但是重定向之后,就不再认证了。

不确定该怎么做,任何想法都将不胜感激。

【问题讨论】:

    标签: node.js express passport.js


    【解决方案1】:

    叹息我真的很愚蠢...答案潜伏在another question 但当我读到它时,我并没有把 2 和 2 放在一起。

    我缺少express-session(这里甚至没有包含我的服务器配置,因此更难调试)。我一设置会话,它就起作用了。

    如果其他人遇到同样的问题,请确保在您的应用配置中包含以下内容:

    var session = require('express-session');
    app.use(session({secret: 'dontderplikeme'});
    

    当您添加护照并包含app.use(passport.session()) 时,这就是它将用来存储凭据的会话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-08
      • 1970-01-01
      • 2014-12-25
      • 2018-11-15
      • 2022-10-06
      • 1970-01-01
      • 2015-02-24
      相关资源
      最近更新 更多