Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cookie-session as non middleware #117

Closed
seromenho opened this issue Jan 7, 2019 · 2 comments
Closed

Use cookie-session as non middleware #117

seromenho opened this issue Jan 7, 2019 · 2 comments

Comments

@seromenho
Copy link

seromenho commented Jan 7, 2019

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

@dougwilson
Copy link
Contributor

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'))

@seromenho
Copy link
Author

Thank you! Works great! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants