Problems integrating multi-tenancy properly
Unanswered
Capple posted this in #help-forum
CappleOP
Hello!
I have a B2B customer portal where users can be in multiple companies and can switch between them. I made a work-around at the time where I returned the user's first valid company with Next-auth's JWT and set it to a Zustand state and used that to swap between the active company which meant that I would need to pass the user's active company uuid to every single request, which caused a lot of bugs in places I forgot to change them etc.
Obviously this is a bad solution and I'm trying to rework the whole thing. I'm in the process of migrating to Better-auth so I'm trying to refactor the whole logic, so that I could just read the user's current company straight from a request itself (route handler or server function).
I'm now torn between saving the activeCompanyUuid to a separate cookie and using that, or saving it to better-auth's own session/cookie. I'm not quite sure what would be the best practice here, I also don't want to use url-based scoping.
And how would I implement either one of them? When and where do I initially set the cookie, how do i make sure the user has active company selected etc? Will I be doing this upon login, in a layout, in a middleware?
Thanks in advance
I have a B2B customer portal where users can be in multiple companies and can switch between them. I made a work-around at the time where I returned the user's first valid company with Next-auth's JWT and set it to a Zustand state and used that to swap between the active company which meant that I would need to pass the user's active company uuid to every single request, which caused a lot of bugs in places I forgot to change them etc.
Obviously this is a bad solution and I'm trying to rework the whole thing. I'm in the process of migrating to Better-auth so I'm trying to refactor the whole logic, so that I could just read the user's current company straight from a request itself (route handler or server function).
I'm now torn between saving the activeCompanyUuid to a separate cookie and using that, or saving it to better-auth's own session/cookie. I'm not quite sure what would be the best practice here, I also don't want to use url-based scoping.
And how would I implement either one of them? When and where do I initially set the cookie, how do i make sure the user has active company selected etc? Will I be doing this upon login, in a layout, in a middleware?
Thanks in advance