Passport js,cookie过期后无法登录 [英] Passport js, can't sign in after cookie has expired

查看:64
本文介绍了Passport js,cookie过期后无法登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用passport js作为身份验证中间件,本地策略.有用.但是当cookie过期后,我不能再登录了.

I'm using passport js as authentication middleware, local strategy. It works. But when the cookie has expired, I can no longer login.

passport.use(new LocalStrategy({
    usernameField: 'email'
  },
  function(username, password, done) {
    mongoose.model('users').findOne({
      email: username,
      password: password
    }, function(err, result){
      if(result){
        var user = {
          name: result.name,
          email:result.email
        };
        return done(null, user);
      }else{
        return done(null, false, { message: 'Incorrect username.' });
      }
    });
  }
));

这是本地策略登录.它正确返回用户.但是在我的浏览器中,我可以看到没有设置 cookie.

This is the LocalStrategy login. It returns the user correctly. But in my browser I can see that no cookie has been set.

如果我重新启动我的节点,它会再次工作.这里有什么问题?

If I restart my node, then it works again. What can be wrong here?

推荐答案

您需要在 express 中设置会话功能.在您的应用配置中应该有这样的内容:

You need to set up session capabilities in express. In your app configuration there should be something like this:

app.use(express.cookieParser()) // must come before session.
app.use(express.session({ secret: 'super hard to guess' }));
app.use(passport.initialize());
app.use(passport.session());

如果您使用 express 4,您将需要两个新的依赖项,express-sessioncookie-parser 模块,因此应该是:

If you're using express 4, you'll need two new dependencies, the express-session and cookie-parser modules, so instead it would be:

var cookieParser = require('cookie-parser');
var session = require('express-session');

app.use(cookieParser());
app.use(session({ secret: 'keyboard cat', key: 'sid', cookie: { secure: true }}));
app.use(passport.initialize());
app.use(passport.session());

这篇关于Passport js,cookie过期后无法登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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