我目前正在尝试使用passport来验证我的node.js Web应用程序。但是,我遇到了正确存储会话数据的问题。目前,它根本没有存储。
App.js
// required for passport
app.use(session({ secret: 'ilovescotchscotchyscotchscotch' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
passport.js
var users = [
{ id: 1, username: 'bob', password: 'secret', email: 'bob@example.com' },
{ id: 2, username: 'joe', password: 'birthday', email: 'joe@example.com' }
];
function findById(id, fn) {
fn(null, users[1]);
}
module.exports = function(passport, db) {
// used to serialize the user for the session
passport.serializeUser(function(user, done) {
console.log("serialization" + JSON.stringify(user, null, 4));
done(null, user.personnel_id);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
findById(id, function (err, user) {
console.log("deserialization" + JSON.stringify(user, null, 4));
done(err, user);
});
});
routes.js
app.post('/login', passport.authenticate('local-login',
{
successRedirect: '/profile',
failureRedirect:'/login',
failureFlash: true,
badRequestMessage: 'Please enter your account credentials to login.'
}));
// =====================================
// PROFILE SECTION =====================
// =====================================
// we will want this protected so you have to be logged in to visit
// we will use route middleware to verify this (the isLoggedIn function)
app.get('/profile', isLoggedIn, function(req, res) {
console.log("req query:" + JSON.stringify(req.query, null, 4));
console.log("req body:" + JSON.stringify(req.body, null, 4));
res.render('profile.jade', {
user : req.user // get the user out of session and pass to template
});
});
在上面的代码中,以下代码按此顺序执行
我可能会忽视一些小事, 任何帮助,赞赏, 谢谢, d
答案 0 :(得分:1)
K没关系。找到了答案。
它存储在req.session.passport ...
下