How do I delete cookies on Node.js when the user leaves the site?

Asked

Viewed 122 times

-1

I am setting a cookie in Node.js when the user chooses the language:

res.cookie('lang', req.body.lang)

But I wanted to delete the cookie when it closes the site, so that when it comes back the language is the default, is it possible to do this? I only found answers to Session cookies.

1 answer

0

The function clearCookie can help you, I’ll give an example:

app.get('/logout', (req, res) => {
// Remove o cachê do usuário
users.splice(
users.findIndex((obj => 
obj.profile.oid == 
req.user.profile.oid)), 1);
req.session.destroy( (err) => {
req.logOut();
res.clearCookie('graphNodeCookie');
res.status(200);
res.redirect('http://localhost:' + port);
 });
});

Source:https://sailsjs.com/documentation/reference/response-res/res-clear-cookie

  • But I wanted to delete the cookies when the user closed the tab for example, I have no option to log out

  • You cannot do this with cookies, only with sessions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.