req.isAuthenticated永远不会更改NodeJS / Passport应用程序

时间:2017-03-11 02:14:38

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

我正在使用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

1 个答案:

答案 0 :(得分:0)

findOne中查询时,请使用find代替LocalStrategy。由于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});
      });
    });
  }));

希望这有帮助。