前preSS 3.0和Passport身份验证
我使用的是passport@0.1.12 ex$p$pss@3.0.0beta4和使用本地srategy进行验证。
I'm using express@3.0.0beta4 with passport@0.1.12 and using local srategy for authentication.
一切似乎都做工精细,并重定向的成功与失败正确
Everything seems to work fine and it redirects on success and failure correctly
app.post('/login', passport.authenticate('local', { failureRedirect: '/' }),
function(req, res) {
console.log(req.isAuthenticated()); // true
res.redirect('/users/' + req.user.id );
});
但是,如果我想补充ensureAuthenticated上的配置文件路径
But if I add ensureAuthenticated on profile route
app.get('/users/:id', ensureAuthenticated, routes.user);
function ensureAuthenticated(req, res, next) {
console.log(req.isAuthenticated()); // false
if (req.isAuthenticated()) { return next(); }
res.redirect('/');
}
它登录后重定向我回'/'(这是登录页),而不是'/用户/ ID(用户配置文件)。问题是req.isAuthenticated()总是返回false并没有req.user变量调试。
it redirects me back to '/' (which is login page) instead of '/users/id' (user profile) after login. The problem is req.isAuthenticated() always return false and there is no req.user variable in debug.
这是与前preSS 3和护照交互问题还是我做错了什么事?
Is it problem with express 3 and passport interaction or I did something wrong?
我有一个类似的问题太多,但事实证明那是因为我是用前preSS会话不指定数据存储会话数据。这意味着会话数据被存储在RAM中,因为我使用多个工人,会话存储并没有工人之间共享。我重新配置为使用 RedisStore
,而是和 isAuthenticated()
开始返回真我的前妻preSS会议如预期
I had a similar problem too, but turns out it was because I was using express sessions without specifying a data store for session data. That meant session data was being stored in RAM, and since I was using multiple workers, the session storage wasn't shared between workers. I reconfigured my express session to use a RedisStore
instead, and isAuthenticated()
started returning true as expected.
app.use express.session
secret: '...'
store: new RedisStore
host: redisUrl.hostname
port: redisUrl.port
db: ...
pass: ...