wasp-lang / wasp

The fastest way to develop full-stack web apps with React & Node.js.
https://wasp-lang.dev
MIT License
12.76k stars 1.14k forks source link

Explore custom session data storage mechanisms #2000

Open sodic opened 2 months ago

sodic commented 2 months ago

One of our Discord members did a great job explaining the feature, so I'll just link that conversation:

Martinsos commented 1 month ago

Some snippets from that Discord convo:

Hi everyone 😉 I'm exploring a possibility to develop my new project in WASP which looks really awesome! However, I've got a question related to auth and was not able to find the answer... In my case user can belong to multiple organisations and switch between them once logged in. I'd like to store the current org ID in session is there any way to do it? I've been using blitz in my previous projects and it has ctx.session.$setPublicData() which is neat (https://blitzjs.com/docs/session-management#customize-session-public-data-in-typescript). Is there any alternative to this in WASP land? Thanks in advance!

Hi @sodic thanks for getting back! To be honest, I've never checked how it's done under the hood (until now) as it comes with a convenient API and just works 🙂 Blitz provides the following APIs:

  • ctx.session.$setPublicData() or setPublicDataForUser() to set public session data in the server side
  • it persists data in the DB sessions table as a json object and also sends back the updated public data in the response cookie as a base64 token which is persisted in both cookies and local storage
  • public session data could be accessed via useSession() in the front-end and e.g. ctx.session.orgId in the back-end

I found it really helpful in some use cases (user roles, multiple orgs per user, etc.). Of course, it's not a show stopper for me as I can implement some alternative myself but it might be a nice addon to WASP. I hope you'll find this useful too. I'm new to WASP but so far so good, nice work guys! 😉