Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node express, how to clear cookie after log out

Basically i'm doing redirect from a.example.com to www.example.com and i expect to be able to delete cookies on www.example.com (because cookie is created with .example.com as the cookie domain), but following code doesn't work.

I know that this question seems like duplicate question, i tried everything from similar question but it doesn't work. See after the code what i already tried.

Using express 3.0.3 and node 0.10.32.

express session middleware

...
var cookiedata = { 
    domain              : '.example.com',
    originalMaxAge      : null,
    httpOnly            : false
};

app.use(express.session({
        store  : ..., 
        secret : ..., 
        key    : 'express.sid', 
        cookie : cookiedata 
}));
...

logout function

function logout(req, res){
    ...

    req.session.destroy(function(){
        req.session = null;

        res.clearCookie('express.sid', { path: '/' });
        res.redirect('https://www.example.com');

    });
}

What i already tried from similar question

  1. https://github.com/strongloop/express/issues/691

So i put path : '/' in express session middleware such as:

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

No success.

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

No success.

like image 514
Srle Avatar asked Aug 20 '15 09:08

Srle


People also ask

How do I clear cookies on Express?

To delete a cookie, use the clearCookie function. For example, if you need to clear a cookie named foo, use the following code. var express = require('express'); var app = express(); app. get('/clear_cookie_foo', function(req, res){ res.

How do I see Nodejs cookies?

We can check cookies by visiting localhost:3000/setcookie. This will show a message as cookies are added.

What does res clearCookie do?

The res. clearCookie() function is used to clear the cookie specified by name. This function is called for clearing the cookies which as already been set. For example if a user cookie is set, then it can be cleared using this function.


2 Answers

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"}

like image 58
swarajpure Avatar answered Sep 18 '22 12:09

swarajpure


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('/');
});
like image 45
Sandro Wiggers Avatar answered Sep 18 '22 12:09

Sandro Wiggers