【问题标题】:req.isAuthenticated never changes in NodeJS/Passport Appreq.isAuthenticated 在 NodeJS/Passport App 中永远不会改变
【发布时间】:2017-08-01 12:24:49
【问题描述】:

我正在使用 NodeJS 构建一个应用程序,并且正在使用 PassportJS 进行用户身份验证。我使用req.isAuthenticated 作为res.render(/some-url function(req, res) {pass in object here}) 中的参数来根据某人是否登录来更改页面的布局。

现在我的页面总是呈现为登录状态,即使我刚刚启动应用程序!

这是我的 Passport 代码:

验证码:

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

passport.deserializeUser(function(id, done) {
  mongo.connect("mongodb://localhost:27017/formulas", function(e, db){
  if (e) {return next(e);}
  var col = db.collection("users");
  col.find({"email": id}, function(err, user){
      done(err, {"email": id});
      });
    });
  });

passport.use(new LocalStrategy({
  usernameField: "email",
  passwordField: "password"
},
function(username, password, done) {
  mongo.connect("mongodb://localhost:27017/formulas", function(e, db) {
  if (e) {return next(e);}
  var col = db.collection("users");
  col.find({"email": username, "password": password}, function(err, user){
    if (err) { return done(err);}
    if(!user) {
      return done(null, false, { message: "Please check your log in   
    credentials." });
    }
    return done(null, {email: username, password: password});
      });
    });
  }));

router.post('/login',
  passport.authenticate('local', {successRedirect:'/',     
  failureRedirect:'/login',failureFlash: false})
);

router.get('/logout', function(req, res){
  req.logout();
  res.redirect('/login');
});

这是我的快速配置的相关部分:

APP.JS:

app.use(cookieParser());
// Express Session
app.use(session({
  secret: 'secret',
  resave: false,
  saveUninitialized: true
}));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

//initialize passport for app
app.use(passport.initialize());
app.use(passport.session());

// make mongodb available to the application
app.use((req, res, next) => {
mongo.connect('mongodb://localhost:27017/formulas', (e, db) => {
  if (e) return next(e);
  req.db = db;
  next();
  });
});

我尝试过为app.use(session{}) 尝试一些不同的配置,但这并没有产生影响。

我用以下方式渲染页面:

router.get(/page-location function(req, res) {
    res.render('view', {
        loggedIn: req.isAuthenticated
    }
});

然后在我看来做

if (loggedIn) {
   //show this stuff
}
if (!loggedIn) {
  //show that stuff
}

它始终显示为loggedIn,即使在我注销后(完成后成功重定向),但由于某种原因req.isAuthenticated 一直显示为true

【问题讨论】:

    标签: node.js express passport.js express-session


    【解决方案1】:

    LocalStrategy 中查询时使用findOne 而不是find。由于find 将始终返回一个数组。所以!user 永远是假的。

    passport.use(new LocalStrategy({
      usernameField: "email",
      passwordField: "password"
    },
    function(username, password, done) {
      mongo.connect("mongodb://localhost:27017/formulas", function(e, db) {
      if (e) {return next(e);}
      var col = db.collection("users");
      col.findOne({"email": username, "password": password}, function(err, user){
        if (err) { return done(err);}
        if(!user) {
          return done(null, false, { message: "Please check your log in   
        credentials." });
        }
        return done(null, {email: username, password: password});
          });
        });
      }));
    

    希望这会有所帮助。

    【讨论】:

    • 谢谢,但是,这个改变并不能解决问题。 FWIW 我已经使用我的原始代码进行了查询,并在我查询时返回了一个带有 req.user 的实际用户以及我的原始代码。问题是,一旦我向/login 发出请求,就好像我陷入了某种循环,req.isAuthenticated 永远不会改变。
    • 我还要补充一点,当我启动我的应用程序时,req.user 会显示为 undefined,所以 req.user 的细节似乎并不能确定问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多