expressjs / cookie-session

Simple cookie-based session middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use cookie-session as non middleware

seromenho opened this issue · comments

I'm trying to get the session from the cookies without using this as a middleware.
Instead I just need to grab session info from the cookies to use it with apollo graphql subscriptions with websockets.
I was trying to parse the cookies and looking at the code to try to find a way to achieve this, but it doesn't seem the correct way of doing it.

Not sure how though, maybe passing just req since is what we have with the ws. Or a check if there's no callback return the Session instead of populating the req.

const session = cookieSession({
  name: 'session',
  keys: [/* secret keys */],

  // Cookie Options
  maxAge: 24 * 60 * 60 * 1000 // 24 hours
})(req)

Would this be possible? I would be happy to contribute if it is.
Or there is an easy way to get the session?

Thanks

btw: just seen this one: apollographql/subscriptions-transport-ws#466

If you just want to get the session contents (and not write back a new session -- which would not be possible without a res anyhow), you could likely just use the cookies module directly, which is what this module uses under the hood to read the cookie and validate the signature. A possible example:

const Cookies = require('cookies')

// …

const cookies = new Cookies(req, null, { keys })
const session = JSON.parse(Buffer.from(cookies.get('session'), 'base64').toString('utf8'))

Thank you! Works great! ❤️