expression3.0和护照authentication
我使用express@3.0.0beta4和passport@0.1.12并使用本地策略进行身份validation。
一切似乎都正常工作,并正确地redirect成功和失败
app.post('/login', passport.authenticate('local', { failureRedirect: '/' }), function(req, res) { console.log(req.isAuthenticated()); // true res.redirect('/users/' + req.user.id ); });
但是,如果我在configuration文件路由添加ensureAuthenticated
app.get('/users/:id', ensureAuthenticated, routes.user); function ensureAuthenticated(req, res, next) { console.log(req.isAuthenticated()); // false if (req.isAuthenticated()) { return next(); } res.redirect('/'); }
它将login后redirect到“/”(login页面)而不是“/ users / id”(用户configuration文件)。 问题是req.isAuthenticated()总是返回false,在debugging中没有req.uservariables。
快递3和护照互动有问题,还是我做错了?
我也有类似的问题,但事实certificate,这是因为我使用快速会议,没有指定会话数据的数据存储。 这意味着会话数据被存储在RAM中,并且由于我使用了多个工作人员,会话存储不在工作人员之间共享。 我重新configuration我的快速会话使用RedisStore
,而isAuthenticated()
开始返回true,如预期。
app.use express.session secret: '...' store: new RedisStore host: redisUrl.hostname port: redisUrl.port db: ... pass: ...
authenticate()是中间件。 从文档:
app.post('/login', passport.authenticate('local', { failureRedirect: '/login' }), function(req, res) { res.redirect('/'); });
问题是,我用curl -L -d "name=Test&password=1"
testing它, curl -L -d "name=Test&password=1"
不能像我所期望的那样工作。 但它使用networking浏览器就可以正常工作。
我也在这个问题上挣扎了好几年。 对我来说固定的是更改会话cookie的maxAge
属性 – 它之前太低了:
app.use(express.cookieParser()); app.use(express.session({ secret: config.session.secret, cookie: { maxAge: 1800000, //previously set to just 1800 - which was too low httpOnly: true } }));
在这个改变之后, req.isAuthenticated()
返回true