我无法使用passort.js和express.js进行注册和登录,本地工作非常好但是当我在远程服务器上部署应用程序时,登录无法在登录后重定向我。它甚至不会闪现失败消息。
我还注意到,当我在远程服务器上进行注册时,它工作正常,但不会闪烁消息。 req.isAuthenticated()始终返回false。 我使用MongoDB作为我的数据源
app.use(session({secret : '3T67774A-R649-4D44-9735-43E296ZZ980F', saveUninitialized: true, resave: true}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
function isLoggedIn(req, res, next) {
console.log("Request Authenticated: " + req.isAuthenticated());
if (req.isAuthenticated())
return next();
res.redirect('/login');
}
passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass in the req from our router (lets us check that a use is logged in or not
}, function(req, email, password, done) {
if (email)
email.toLowerCase(); // use lower-case emails to avoid case-sensitive email matching
// asynchronous
process.nextTick(function() {
User.findOne({'local.email' : email}, function(err, user) {
// if there are any errors, return the error
if (err)
return done(err);
// if no user found, return the message
if (!user)
return done(null, false, req.flash('loginMessage', 'User not found.'));
if (!user.isValidPassword(password))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));
// return user
else
return done(null, user);
});
});
}));