Deleting cookies

6,105 views
Skip to first unread message

ilya

unread,
Aug 13, 2011, 10:16:41 AM8/13/11
to expre...@googlegroups.com
I see that it's possible as describes here - https://github.com/visionmedia/express/issues/314, but I just can't do it.

How, exactly, would I delete a cookie?

In my code, I do this:

app.get('/logout', function (req, res, next) {
  req.cookie('mycookie', null);
  res.end();
});

but get 

TypeError: Object #<IncomingMessage> has no method 'cookie'

What is the correct way?

Darrell Banks

unread,
Aug 13, 2011, 3:38:34 PM8/13/11
to expre...@googlegroups.com

res.clearCookie('cookieName')

> --
> You received this message because you are subscribed to the Google Groups "Express" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/express-js/-/rA8KzrML7ckJ.
> To post to this group, send email to expre...@googlegroups.com.
> To unsubscribe from this group, send email to express-js+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/express-js?hl=en.
>

ilya

unread,
Aug 14, 2011, 5:34:09 AM8/14/11
to expre...@googlegroups.com
Great, thank you!

Darrell Banks

unread,
Aug 14, 2011, 10:34:01 AM8/14/11
to expre...@googlegroups.com
Glad I could help! : )

On Sun, Aug 14, 2011 at 5:34 AM, ilya <ilya.sha...@gmail.com> wrote:
Great, thank you!

--
You received this message because you are subscribed to the Google Groups "Express" group.
To view this discussion on the web visit https://groups.google.com/d/msg/express-js/-/nY6a-ODX-ZwJ.

ilya

unread,
Aug 19, 2011, 1:51:23 PM8/19/11
to expre...@googlegroups.com
Actually, it doesn't work :/

This code
...
// stuff
app.use(express.session({ store : store, secret : session_secret, cookie: {path: '/', httpOnly: true, maxAge: 604800000}}))
// stuff
...
app.get('/logout', function (req, res, next) {
  req.session.destroy();
  res.clearCookie('connect.sid');
  res.clearCookie('mycookie'); // <-- this cookie is NOT httpOnly, used by client-side JS
  res.setHeader('Location', '/');
  res.end();
});

doesn't do anything. I see there's this topic, but it's pretty old and I suppose it's fixed?..
I'm looking at cookies in Firefox and they just stay.

What else could it be?

Darrell Banks

unread,
Aug 19, 2011, 2:11:33 PM8/19/11
to expre...@googlegroups.com

Which version of express / node are you running?

On Aug 19, 2011 1:51 PM, "ilya" <ilya.sha...@gmail.com> wrote:
> Actually, it doesn't work :/
>
> This code
> ...
> // stuff
> app.use(express.session({ store : store, secret : session_secret, cookie:
> {path: '/', httpOnly: true, maxAge: 604800000}}))
> // stuff
> ...
> app.get('/logout', function (req, res, next) {
> req.session.destroy();
> res.clearCookie('connect.sid');
> res.clearCookie('mycookie'); // <-- this cookie is NOT httpOnly, used by
> client-side JS
> res.setHeader('Location', '/');
> res.end();
> });
>
> doesn't do anything. I see there's this topic<https://groups.google.com/d/topic/express-js/PmgGMNOzhgM/discussion>,
> but it's pretty old and I suppose it's fixed?..
> I'm looking at cookies in Firefox and they just stay.
>
> What else could it be?
>
> --
> You received this message because you are subscribed to the Google Groups "Express" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/express-js/-/xRwQlA7-ETcJ.

ilya

unread,
Aug 19, 2011, 2:38:13 PM8/19/11
to expre...@googlegroups.com
Node 0.4.10 and just updated Express to 2.4.5 but the issue persist. But I found the problem and it's two-part.

First, the routes.
This is my route:  app.get('/logout', ...).
When I go (in the browser) to http://site.com/logout - I get redirected, which is the correct behaviour but the cookies stay intact (and I changed code to do res.redirect('/index.html') ).
However, when I go to http://site.com/logout/ - notice the trailing slash - I'm redirected and cookies are cleared. Trailing slash makes the difference. Maybe because of the isDirectory() check?

Second, the cookie clearing method.
I had to changed the code to
  res.cookie('connect.sid', '', {expires: new Date(1), path: '/' });
  res.cookie('mycookie', '', {expires: new Date(1), path: '/', httpOnly: false});
because res.clearCookie() doesn't work, even with trailing slash. I don't know, maybe TJ forgot to push the changes?..

Anyway, the final working code is
app.get('/logout/', function (req, res, next){
  req.session.destroy();
  res.cookie('connect.sid', '', {expires: new Date(1), path: '/' });
  res.cookie('mycookie', '', {expires: new Date(1), path: '/', httpOnly: false});
  res.redirect('/index.html');
});

Maybe someone will find this useful.

TJ Holowaychuk

unread,
Aug 19, 2011, 2:41:22 PM8/19/11
to expre...@googlegroups.com
clearCookie() should be fine, it essentially does the same as what you're solution was. res.clearCookie() invokes this.cookie(name, '', merges options here); in turn this.cookie() defaults the path to "/" etc

-- 
TJ Holowaychuk
--
You received this message because you are subscribed to the Google Groups "Express" group.
To view this discussion on the web visit https://groups.google.com/d/msg/express-js/-/_r9MH4FrzCcJ.

ilya

unread,
Aug 19, 2011, 2:45:59 PM8/19/11
to expre...@googlegroups.com
Just tested code with
  res.clearCookie('connect.sid');
  res.clearCookie('mycookie');
and it doesn't work. Honestly.

mockee

unread,
Sep 3, 2011, 6:58:16 AM9/3/11
to Express
// u can try this
res.clearCookie('connect.sid', { path: '/' });
res.clearCookie('mycookie', { path: '/' });

ktmud

unread,
Jan 20, 2012, 10:49:23 AM1/20/12
to expre...@googlegroups.com
yay! This works!

max

unread,
Jul 27, 2015, 12:12:21 PM7/27/15
to Express, moc...@gmail.com
This worked for me, cheers !

res.clearCookie('connect.sid', { path: '/' }); 

Max

pandu s

unread,
Jun 3, 2016, 1:30:04 AM6/3/16
to Express
How we can delete cookies.

 app.get('/logout', function(req, res) {
req.session.destroy(function(){
      res.clearCookie('connection.sid');
         res.redirect('/');
           
    });
req.logout();
}); 


I have tried all the possible ways 

app.get('/logout/', function (req, res, next){
  req.session.destroy();
   res.cookie('connect.sid', '', {expires: new Date(1), path: '/' });
  res.cookie('mycookie', '', {expires: new Date(1), path: '/', httpOnly: false}); 
  /* res.clearCookie('connect.sid', { path: '/' }); 
   res.clearCookie('mycookie', { path: '/' });  */
  res.redirect('/');
   req.logout();
});

But still not working , pleases suggest me how can we remove cookies .
Reply all
Reply to author
Forward
0 new messages