FoalTS / foal

Full-featured Node.js framework, with no complexity. 🚀 Simple and easy to use, TypeScript-based and well-documented.
https://foalts.org/
MIT License
1.89k stars 140 forks source link

Allow session stores to access information from the current request #1038

Open luxaritas opened 2 years ago

luxaritas commented 2 years ago

I'm implementing a multi-tenant app where each tenant has its own database, and sessions should also be split between each database. I've written a custom session store, but as I am passing the tenant ID as a path parameter, the session store has no way to determine which database to use. For now, I have to basically re-implement sessions.

luxaritas commented 2 years ago

I actually found a different way around this than re-implementing sessions. What I did was when returning the token to the user on login, I formatted the token to include the tenant ID, and then in my session store's read method parsed it out. Then I also similarly serialized the user ID to include both the user ID and tenant ID, so that I could access it for save, update, and fetchUser operations. It's still a bit hacky and it would probably still be ideal to provide extra request-time data directly, but this is at least doable.