expressjs / session

Simple session middleware for Express
MIT License
6.27k stars 979 forks source link

Get session object from `req.session` outside of request context #979

Closed RixInGithub closed 7 months ago

RixInGithub commented 7 months ago

Hello, Express support team. (I don't really know who am I asking :P)

I wanted to ask you one simple thing.

Let's say I got a session.sid cookie pop out from nowhere. And, of course, I want to get the Session object from the cookie value I somehow got.

I know, a very hypothetical question with no samples. And also a short one.

Can you help?

RixInGithub commented 7 months ago

Nevermind. Found a solution, personal.

RixInGithub commented 7 months ago

My solution:

Get a MemoryStore from new (require("express-session/session/memory"))() and give it as the store option, then store.load the cookie value.

RixInGithub commented 7 months ago

UPDATE: After looking on NPM published source, you need to unsign the cookie using require("cookie-signature").unsign(YOURSIDHERE.slice(2)), and also use store.get instead of store.load