node express, how to clear cookie after log out
This is response.clearCookie of Express.JS (file response.js at line 749).
var opts = merge({ expires: new Date(1), path: '/' }, options);return this.cookie(name, '', opts);
If you set a breakpoint at this line you will see expires is reported at an invalid date. So instead of using response.clearCookie, just make it expire immediately like this one.
response.cookie("express.sid", "", { expires: new Date() });
This is working for me with cookie-parser
module:
router.get('/logout', function(req, res){ cookie = req.cookies; for (var prop in cookie) { if (!cookie.hasOwnProperty(prop)) { continue; } res.cookie(prop, '', {expires: new Date(0)}); } res.redirect('/');});
What worked for me was adding path and domain in res.clearCookie
res.clearCookie(<cookie-name>, {path: '/', domain: <domain-on-which-cookie-is-set>}
Also, make sure to include credentials on the frontend, otherwise no cookie will be sent with the request. If no cookie goes to the server, it has nothing to clear!
fetch('url.com', {credentials: "include"}