Storing additional data in session
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!8 Replies
Hi @aslauris. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Hello! Wasp's auth uses sessions to keep track of the logged-in user. The session is stored in
localStorage
on the client and in the database on the server source (https://wasp-lang.dev/docs/auth/overview#sessions).
However, as of the current state of Wasp, there isn't a direct equivalent to Blitz's ctx.session.$setPublicData()
.
That said, Wasp does allow you to customize the signup process by defining extra fields. This could potentially be used to store additional information about the user, such as the current organization ID source (https://wasp-lang.dev/docs/auth/overview#1-defining-extra-fields).
Additionally, there is an open issue on GitHub discussing the possibility of providing a managed client storage solution, which could potentially be used to store additional user-specific data source (https://github.com/wasp-lang/wasp/issues/1082).
For more complex scenarios, you might consider setting up a custom Wasp API endpoint that can query the user info source (https://discord.com/channels/686873244791210014/1214656571787710474).
Please note that these are potential solutions and might require additional customization to fit your specific use case. It's always a good idea to follow the ongoing discussions and updates in the Wasp community for the latest features and improvements.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @aslauris, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?@sodic another one to pick up when you have the time
Hey @aslauris, Wasp doesn't support this out of the box.
But you can let me know how you'd want to use it (e.g., is it persistent in the database or just a property of a browser session that doesn't need to be stored anywhere)
And we can come up with a way to best implement it
Both for Wasp in the future and for you now π
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! πThis is great stuff, @aslauris!
Thanks for digging in and getting all the details.
Yes, we definitely have no such system. Also, yes, you can 100% do this yourself (let me know if you need any help), but it's pretty manual at this point.
I'll create a GitHub issue for exploring this system and link your message if that's OK with you π
Sure thing @sodic !
Here's the issue, it even got a nice round number: https://github.com/wasp-lang/wasp/issues/2000
Sweet β€οΈ