passportjs:user undefined,req.isAuthenticated()为false

时间:2017-10-02 23:49:29

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

注意:有许多类似的问题,但我已经完成了所有这些问题,但没有一个解决方案有效。

您好, 我已经尝试过网络上的所有解决方案,但bug仍然存在。

我正在构建一个简单的CRUD应用程序,而我目前正在努力使用passportjs的req.isAuthenticated()。 用户肯定是经过身份验证的,因为passport.authenticate中的console.log(req.user)返回用户对象,而req.session.passport具有用户ID。

重定向后,即使重定向是异步,用户也是未定义的,req.session.passport为空。这就好像会话被重置一样。

以下是代码:

app.js



var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var session = require('express-session');
var passport = require('passport');
var flash = require('connect-flash');

var index = require('./routes/index');
var users = require('./routes/users');

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

app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

app.use(express.static(path.join(__dirname, 'public')));
app.use('/bootstrap', express.static(path.join(__dirname + '/node_modules/bootstrap/dist')));

app.use(session({
  secret: 'keyboard cat',
  resave: true,
  saveUninitialized: true,
  cookie: {
    maxAge: 5 * 60 * 1000
  }
}));

app.use(passport.initialize());
app.use(passport.session());
app.use(flash());

app.use(function(req, res, next) {
  res.locals.messages = require("express-messages")(req, res);
  next();
});

app.get('*', function(req, res, next) {
  console.log(req.method, ":", req.url);
  res.locals.user = req.user || null;
  next();
});

app.use("/", index);
app.use("/users", users);

var port = 4000;
app.listen(port);
module.exports = app;




index.js



var express = require('express');
var router = express.Router();

router.get('/', isLoggedIn, function(req, res) {
  if (req.user) {
    console.log(req.user.username);
  }
  res.render('homepage');
});

function isLoggedIn(req, res, next) {
  console.log("passport: ", req.session.passport); //passport:{}
  if (req.isAuthenticated()) {
    return next();
  }
  res.redirect('/users/login');
}

module.exports = router;




users.js



var express = require('express');
var router = express.Router();
var mongojs = require('mongojs');
var db = mongojs('usersDB', ['users']);
var bcrypt = require('bcryptjs');
var passport = require('passport'),
  LocalStrategy = require('passport-local').Strategy;​
router.get('/login', function(req, res) {
  res.render('login');
});​
router.get('/signup', function(req, res) {
  res.render('signup');
});​
router.post('/signup', function(req, res) {
  db.users.findOne({
    username: req.body.name
  }, function(err, user) {
    if (err) {
      return done(err);
    }
    if (user) {
      console.log("nope");
      req.flash("error", "Username taken. Choose a different username.");
      return res.redirect('/users/signup');
    }
    var password = req.body.password;
    bcrypt.genSalt(10, function(err, salt) {
      if (err) {
        return done(err);
      }
      bcrypt.hash(password, salt, function(err, hash) {
        if (err) {
          return done(err);
        }
        var user = {
          username: req.body.name,
          email: req.body.email,
          phone: req.body.phone,
          password: hash
        }
        db.users.insert(user, function(err, result) {
          if (err) {
            return done(err);
          }
          console.log("registered", user)
          res.render("homepage", {
            user: user
          });
        });
      });
    });
  });
}); //sorry for the callback hell..works fine
​​
passport.serializeUser(function(user, done) {
  return done(null, user._id);
});​
passport.deserializeUser(function(id, done) {
  db.users.findOne({
    id: mongojs.ObjectId(id)
  }, function(err, user) {
    return done(err, user);
  });
});​
passport.use('loginStrategy', new LocalStrategy({
    passReqToCallback: true,
    usernameField: 'username',
    passwordField: 'password'
  },
  function(req, username, password, done) {
    db.users.findOne({
      username: username
    }, function(err, user) {
      if (err) {
        return done(err);
      }
      if (!user) {
        return done(null, false, req.flash('error', "Invalid username."));
      }
      bcrypt.compare(password, user.password,
        function isMatch(err, isMatch) {
          if (err) {
            return done(err);
          }
          if (!isMatch) {
            return done(null, false, req.flash('error', "Oops. Incorrect password."));
          }
          return done(null, user, req.flash('success', "Welcome " + user.username + ", you are now logged in."));
        });
    });
  }​));​
router.post('/login', function(req, res, next) {
  passport.authenticate('loginStrategy', function(err, user, info) {
    if (info) {
      req.flash('error', info.message);
    }
    if (err) {
      return next(err);
    }
    if (!user) {
      return res.redirect('/users/login');
    }
    req.logIn(user, function(err) {
      if (err) {
        return next(err);
      }
      //callback redirect after saving session.
      req.session.save(function() {
        console.log(req.session.passport); //{ user: 59ceb263dae7a4270087ae57 }
        res.redirect('/');
      });
    });
  })(req, res, next);
});​
router.get('/logout', function(req, res) {
  req.logout();
  res.redirect('/users/login');
});​
module.exports = router;




我真的很感激一些帮助,因为这似乎是一个常见的错误,但没有一个解决方法适合我。

1 个答案:

答案 0 :(得分:0)

这似乎与您用于存储用户信用的方法有关。请注意,Chrome和IE有关于本地和会话存储的不同怪癖。在chrome中尝试这一点,Passport JS会将响应存储在Local或Session存储中 - 您应该询问这些存储机制以验证用户的凭据是否在其中。

在chrome中,转到F12调试工具并查看会话和本地存储:

enter image description here

请告诉我们您使用的浏览器,以及使用Chrome有何不同。