【发布时间】:2020-07-14 19:33:14
【问题描述】:
我正在将 Passport 集成到我的 NodeJs APP 中并且注册有效,登录本身有效,但是在登录后直接重定向时,我的 Session.Passport 再次为空,并且永远不会调用 deserializeUser。也许有人看到我在哪里做错了,或者知道我做错了什么。
首先我的 Passport 设置在我的 app.js 中:
app.use(session({ cookie: { maxAge: 18000 },
secret: cryptoSecret,
resave: false,
saveUninitialized: true,
secure: false,
store: new memoryStore({checkPeriod: 86400000}),
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(passport.initialize());
app.use(passport.session());
我的本地策略等:
// =========================================================================
// passport session setup ==================================================
// =========================================================================
// required for persistent login sessions
// passport needs ability to serialize and unserialize users out of session
// used to serialize the user for the session
passport.serializeUser(function(user, done) {
console.log('Serialize User');
console.log(user);
console.log('---------------------------------------');
console.log(user._id);
console.log('---------------------------------------');
done(null, user._id);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
console.log('deserialize Important!');
User.findById(id, function(err, user) {
console.log(err);
done(err, user);
});
});
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 back the entire request to the callback
},
function(req, email, password, done) { // callback with email and password from our form
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'local.email' : email }, function(err, user) {
// if there are any errors, return the error before anything else
if (err)
return done(err);
// if no user is found, return the message
if (!user)
return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
// if the user is found but the password is wrong
if (!user.validPassword(password))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
// all is well, return successful user
return done(null, user);
});
}));
};
这是我的登录路径:
router.post('/', cors(), passport.authenticate('local-login', { failureRedirect : '/', failureFlash : true}), (req, res) => {
console.log('Login route! Authenticated?:', req.isAuthenticated(), ' Session:' , req.session);
req.logIn(req.session.passport.user, err => {
if(err) {
console.log(err);
}
res.redirect('/group');
});
});
使用 successRedirect 而不是 res.redirect 时,我得到了相同的行为:/
认证后直接会话:
重定向时我的会话:
我追踪到我的反序列化用户函数没有被调用,并阅读了这么多 SO 条目和其他网站,没有任何变化。有点沮丧的atm。 首先感谢大家的阅读和帮助。我感谢所有可能对我有帮助的信息。我希望你们都有美好的一天:)
【问题讨论】:
标签: node.js express passport.js