Express session expire after page refresh

I have an app. When the user goes to the home page of the application, a new session is created with the ID, and never expires, I checked how my application works on the local computer, and everything works as it should, after going to the main page of the application, a session with the id is created and never expires.

But when I run this app in glitch, it doesn’t work correctly, session of my nodejs app is expiring every time I refresh the page. Each time the page is reloaded, a new session is created with a new ID. Here is my code:

    app.set('trust proxy', 1);
    app.use(session({
     cookie: {
        httpOnly: true,
        sameSite: true,
        secure: true
     },
     resave: false,
     saveUninitialized: true,
     store: new MongoStore({mongooseConnection: db}),
     secret: process.env.secret,
     name: 'ses'
    }));

mainRoute:

    app.get('/', (req, res)=>{
        res.sendFile(__dirname+'/public/index.html');
        req.session.a = "some data";
    });

Sorry for my English. I will be glad if someone helps !! :slight_smile:

I’ve experienced the same issue when using fastify on Glitch, you should be able to resolve it by setting secure to false in the session cookie config.

Thank you, I tried it, but nothing changed :frowning:

I just changed the “SameSite” property to “false” and it worked !!! Does anyone know why after I changed the “SameSite” property to “false” it started working ?)

Edit: I would like to set “sameSite” property to “strict”, but the cookies will not be set in Glitch

1 Like