Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js passport session cookie domain

I'm using node.js + passport and trying to figure out how the set the cookie on the parent domain so that it's available to sub domains.

User logs into Domain.com User then goes to Sub.Domain.com ... he should still be logged in.

how does one set the cookie on the parent domain? Here's what I currently have.

app.use(express.session({
   secret: 'XXXXX',
   store: new mongoStore({ url: app.get('mongodb-uri') })
}));
app.use(passport.initialize());
app.use(passport.session());
like image 530
Warrick FitzGerald Avatar asked Oct 28 '25 05:10

Warrick FitzGerald


1 Answers

What you need to do is set the domain of the session cookie. You should be able to do this like:

app.use(express.session({ 
  secret: <session_secret> , 
  store: <session store> ,
  cookie: {
    path: '/',
    domain: '.domain.com',
    maxAge: 1000 * 60 * 24 // 24 hours
  }
}))

Notice that the domain was set to .domain.com (the dot at the beginning) which should make it available to all subdomains.

like image 69
Nick Mitchinson Avatar answered Oct 31 '25 01:10

Nick Mitchinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!