Michael Seltenreich Michael Seltenreich - 3 months ago 5
Node.js Question

Why is my session expiring every time I close the browser?

I set up the session maxAge of express like documented.

Here is my code:

app.use(session({
secret: process.env.SESSION_SECRET,
saveUninitialized: true,
resave: true,
maxAge: 1000* 60 * 60 *24 * 365,
store: new MongoStore({mongooseConnection:mongoose.connection})
}));


But every time I close the browser, I find myself logged out.

Also, note that I am using Passport local, facebook, and google authentications.

They all expire.

In the console, I can see that the connect.sid in the expires/maxAge section lists "Session" while other cookies have dates...

What am I doing wrong?

Answer

you need to configure your express-session, and set maxAge on session-cookie

app.use(express.session({
    cookie : {
        maxAge: 1000* 60 * 60 *24 * 365
    },
    store : new MongoStore({mongooseConnection:mongoose.connection})
});
//.....
app.use(passport.session());
Comments