Search code examples
javascriptnode.jsexpresssessionsession-cookies

Deployed express server isn't creating a session?


I have deployed a node express server on digital ocean. However, it won't create the session once it's deployed. I added a store to prevent memory leak and initialized app.set('trust proxy', 1), before the session. I am creating a user for the session once the user is authenticated. The snippet below shows the configuration for the session.

app.use(
    session({
        key: "userid",
        secret: "subscribe",
        resave: false,
        saveUninitialized: false,
        store: sessionStore,
        cookie: {
            expires: 60000 * 60,
            domain: ".section-webapp-y793v.ondigitalocean.app"
        }
    })
)

app.get("/login", (req, res) => {
    if (req.session.user) {
        res.send({ loggedIn: true, user: req.session.user })
    } else {
        res.send({ loggedIn: false })
    }
}
)

Solution

  • Found out the issue was with the server. Since I had applied secure:true, it wouldn't work since the production server wasn't secured with SSL. Then I also changed the cookies to sameSite:none.

    app.use(
        session({
            key: "userid",
            secret: "subscribe",
            resave: false,
            store: sessionStore,
            saveUninitialized: false,
            cookie: {
                expires: 60000 * 60,
                secure: true, 
                sameSite: "none"
            }
        })
    )```