Skip to content Skip to sidebar Skip to footer

Nodejs And Expressjs Cannot Set Cookie

Can you help me for creating cookies cause i can't make it work. I would like to set and create cookies after the user logs in but I don't know what's wrong with my codes. Thanks G

Solution 1:

To use cookies you should look at the Express cookieSession middleware. The docs are here: http://expressjs.com/api.html#cookieSession

Update (I can't test it right now so this is from what I can remember):

You can add the cookieSession middleware and specify the settings for your cookie with something like:

app.use(express.cookieSession({
    cookie: {
        path: '/',
        maxAge: 3600000
    }
}));

Then when your user logs in you can set the login token on the session with req.session.login_token = 'login_token';.

And when your user logs out you can clear the session by doing req.session = null;.

In your authentication middleware you can then check that the login token is set on the session to see if the user is authenticated.

Post a Comment for "Nodejs And Expressjs Cannot Set Cookie"