Node express how to clear cookie after logout

Basically, I am redirecting from a.example.com to www.example.com and I expect that I can delete cookies on www.example.com (because the cookie was created with .example.com as the cookie domain), but The following code does not work.

I know that this question looks like a repeating question, I tried everything from a similar question, but it does not work. Look after the code that I have already tried .

Using expressions 3.0.3 and node 0.10.32.

session middleware

... var cookiedata = { domain : '.example.com', originalMaxAge : null, httpOnly : false }; app.use(express.session({ store : ..., secret : ..., key : 'express.sid', cookie : cookiedata })); ... 

exit function

 function logout(req, res){ ... req.session.destroy(function(){ req.session = null; res.clearCookie('express.sid', { path: '/' }); res.redirect('https://www.example.com'); }); } 

What have I tried from a similar question

So, I put path : '/' in the express session middleware, for example:

 app.use(express.session({ ..., path : '/' }); 

No success.

  1. https://groups.google.com/forum/#!topic/express-js/PmgGMNOzhgM
    Instead of res.clearCookie, I used: res.cookie ('express.sid', '', {expires: new Date (1), path: '/'});

No success.

+8
javascript cookies express
source share
2 answers

This is response.clearCookie of Express.JS (response.js file on line 749).

 var opts = merge({ expires: new Date(1), path: '/' }, options); return this.cookie(name, '', opts); 

If you set a breakpoint on this line, you will see that the message expires with an invalid date. So instead of using response.clearCookie, just run it immediately, like this one.

 response.cookie("express.sid", "", { expires: new Date() }); 
+6
source share

This works for me with the 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('/'); }); 
+2
source share

All Articles