前preSS 3.0和Passport身份验证 [英] express 3.0 and passport authentication

查看:158
本文介绍了前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: ...

这篇关于前preSS 3.0和Passport身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆