Set cookie for domain instead of subDomain using NodeJS and ExpressJS

49,369

Solution 1

configure it like this:

app.use(express.session({
    secret: conf.secret,
    cookie: { domain:'.yourdomain.com'},
    store: new MongoStore(conf.sessiondb)
}));

Solution 2

Try to use the following link to configure.

res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true });

Link : http://expressjs.com/api.html#res.cookie

Share:
49,369
Raja
Author by

Raja

Senior Front-End UI Developer with 14+ years of experience in Full-Stack web application development with LAMP Environment, Angular 6+, PHP (V 7.1 & 5.6), Angular 6+, Type Script, SQL (generic SQL, MySQL, Oracle), Perl, Oracle, MongoDB, HTML5, XHTML, CSS, JavaScript (generic JS, jQuery, Ajax, cross-domain Ajax), Bootstrap, REST API, XML and JSON. Also, I have strong knowledge background in project management which includes Requirement gathering and analysis, Estimation, Design, Development, Code review, Testing and coordinating with stakeholders and off-shore team. Passionately work towards enhancing project knowledge of self and the team. Able to perform as an amicable team player as well as individual contributor. Client coordination and working closely with the project team (DEV and QA).

Updated on August 17, 2020

Comments

  • Raja
    Raja over 3 years

    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?