So what im attempting to do is to set a cookie on domain.com which is available to all subdomains *.domain.com. Im currently using Express 4.x and NodeJS.
At the moment I can set and get any Cookies on the main domain in my case its testing on a local environment via lvh.me:3000 (allowing for local subdomains)
This is bascially what my Express.js App looks like:
// Require all files (express,http,express-session,cookie-parser...)
var api = express()
// Cookie Parser
api.use(cookieParser());
api.use(session({
secret: 'yoursecret',
cookie: {
path: '/',
domain: '.lvh.me:3000',
maxAge: 1000 * 60 * 24, // 24 hours
},
resave: true,
saveUninitialized: true
}));
// CORS
api.use(function(req, res, next) {
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
next()
})
// View Engine
api.set('views', './api/views');
api.set('view engine', 'ejs');
api.disable('x-powered-by');
// Stylus
function compile(str, path) {
return stylus(str).set('filename', path);
}
api.use(stylus.middleware({
src: './app',
compile: compile
}));
// Serving Static Content
api.use(express.static('./app'));
// All Routes
require('../api/routes/test.server.routes.js')(api);
// Router
api.get('*', function(req, res) {
//res.cookie('remember', 1, { domain : "lvh.me:3000" });
res.render('index'); // Get all requests to Index. Angular takes over routing.
});
// Return Application
return api;
Cookies are set via Cookie Parser res.cookie('user', 'bobby'); which seem to be fine for going to lvh.me:3000/anything but for boddy.lvh:3000/anything the Cookies are empty.
Am I missing anything here, I thought Cookies would be available across all subdomains? I have read a few Articles/Posts on Stack Overflow but they all seem rather outdated. Any guidance or help is greatly appreciated.
On a side note if i set a cookie within express file it will be available through out the application. For Example:
// Router
api.get('*', function(req, res) {
res.cookie('user', 'jim'); // This can be accessed
res.render('index');
// Get all requests to Index. Angular takes over routing.
});
Any reason why? - This is in the most part because its being set on any/every view. So it still leaves me with the original question.