Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set cookie for domain instead of subDomain using NodeJS and ExpressJS

I have been using expressjs and mongostore for session management. Following is the code to configure store in expressjs,

app.configure(function(){     app.use(express.session({         secret: conf.secret,         maxAge: new Date(Date.now() + 3600000),         cookie: { path: '/' },         store: new MongoStore(conf.db)     })); }); 

I had mentioned the cookie path in the above code. But it sets the cookie in sub.domain.com instead of .domain.com. How do i achieve this?

like image 999
Raja Avatar asked Oct 20 '11 09:10

Raja


People also ask

Can a domain set a cookie for a subdomain?

Please everyone note that you can set a cookie from a subdomain on a domain. But you CAN'T set a cookie from a domain on a subdomain.

Can you set cookies for another domain?

Setting cookies for another domain is not possible. If you want to pass data to another domain, you can encode this into the url.

How can express set a cookie on the client?

In order to correctly set cookies accessible on the client just use a snippet like the following: res. cookie('rememberme', 'yes', { maxAge: 900000, httpOnly: false});


1 Answers

configure it like this:

app.use(express.session({     secret: conf.secret,     cookie: { domain:'.yourdomain.com'},     store: new MongoStore(conf.sessiondb) })); 
like image 74
metis Avatar answered Sep 22 '22 12:09

metis