问题
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