Passport js, can't sign in after cookie has expired

假如想象 提交于 2019-12-13 05:18:18

问题


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.' });
      }
    });
  }
));

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?


回答1:


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());

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());


来源:https://stackoverflow.com/questions/23591982/passport-js-cant-sign-in-after-cookie-has-expired

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!