Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ExpressJS: set/delete cookies
// Dependencies.
var express = require('express')
app = module.exports = express.createServer(),
del = function(req, res) { res.clearCookie('login_token'); res.redirect('/'); },
set = function(req, res) { res.cookie('login_token', +new Date(), { maxAge: 3600000, path: '/' }); res.redirect('/'); };
// Config
app.configure(function() {
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(app.router);
});
// Routes
app.get('/', function home(req, res) {
res.end('<a href="/set">set</a> <a href="/del/a">del</a> <a href="/del">rlydel</a>');
console.log(req.cookies);
});
app.get('/set', set);
app.get('/del/a', del);
app.get('/del', del);
// Server
app.listen(3000);
@tj

This comment has been minimized.

Copy link

@tj tj commented May 31, 2011

will check this out in a minute

@codepunkt

This comment has been minimized.

Copy link
Owner Author

@codepunkt codepunkt commented May 31, 2011

no clue if its meant to be this way or if its a node problem. express response code seems fine to me :)

@tj

This comment has been minimized.

Copy link

@tj tj commented May 31, 2011

it works if you add { path: '/' } to the clearCookie(). The browser should default the path to whatever the current path is but maybe internally we should default to "/" which I would at least consider to be the usual case (aka always available)

@tj

This comment has been minimized.

Copy link

@tj tj commented May 31, 2011

at least that way the "global" cookie typical use-case would be cleaner:

res.cookie('foo', 'bar', { maxAge: n });
res.clearCookie('foo');
@codepunkt

This comment has been minimized.

Copy link
Owner Author

@codepunkt codepunkt commented May 31, 2011

those 2 lines could then be called wherever one would like and both default to '/', right?

@tj

This comment has been minimized.

Copy link

@tj tj commented May 31, 2011

yeah

@codepunkt

This comment has been minimized.

Copy link
Owner Author

@codepunkt codepunkt commented May 31, 2011

sounds good :)

@tj

This comment has been minimized.

Copy link

@tj tj commented May 31, 2011

posted this on the ML for more feedback

@codepunkt

This comment has been minimized.

Copy link
Owner Author

@codepunkt codepunkt commented Jun 1, 2011

@VinuOye

This comment has been minimized.

Copy link

@VinuOye VinuOye commented Jan 9, 2015

In my project am Sign In through Google+ like this

untitled

When we click on goolge+ icon it asking emailId and password of gmail like this

untitled2

after we click sign in it will ask allow access to our project. then it will signin Successfully to our project. like this

untitled1

when we signout and again click on google+ sign in icon it directly logged in to our project without asking email Id and password. am tried lot of things like clearCookies, session.destroy like etc... but it doesn't asking the form i.,e email Id and password of gmail.

After when we click the clear cookie addon on firefox, like this

untitled3

it will ask the form for login access like this

untitled2

please help me.
am using passport and expressjs 4.0 on server.

@trananhtuyen

This comment has been minimized.

Copy link

@trananhtuyen trananhtuyen commented Aug 20, 2015

in nodejs module request or express have function curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); code PHP

@wzup

This comment has been minimized.

Copy link

@wzup wzup commented Jul 5, 2016

How to clear all cookies programmatically? Not only those you explicitly set yourself in your app with `res.cookie(...), but also those ones that third-party services set themselves, like FB during login with FB. Is it possible at all? This is for the sake of testing, to clear all cookies, that is reset the state in order to start a test again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment