T3 - pass current tenant slug into TRPC context
I'm trying to create a Saas product with T3 and I need to have the tenant slug in the TRPC context so that I can try to do the operations in the correct tenant.
I was able to use headers from the react client to pass in the slug, but I don't know a good way to solve this for server components.
Does anyone have a good example or can provide some guidance for this?
8 Replies
my only solution is to always create the api with the params.slug, but I wonder if theres a way to inject it via custom headers (as I do in the FE client)
whats this about?
i think the tenant slug is like the users's team's name?
i store that in the session
yeah its the team's slug in the URL.
you store it a cookie? when do you update it? on a GET request to the /[slug] layout?
using session/cookies doesn't mean that a user can't use different "tenants" in separate tabs?
i use the session object, since it's called all the time and i don't want to make additional API calls for each request
- using session/cookies doesn't mean that a user can't use different "tenants" in separate tabs? - maybe, it hasn't been a requirement i've faced - LMK how you do it
So far I'm being always getting the slug from the URL.
in client components I add it to the headers in the
TRPCReactProvider
.
in server components, instead of using the api that is statically created, I have to call a createAPI(slug)
method that creates the API and adds the slug to the headers.
And then, finally, in the createContext I query the team data with the slug I get from the headers, and add it to the TRPC context.what about a spoof?
what do you mean? if someone puts a different team's slug in the url? my query ensures that the user is authorized to get that team's data
i haven't done this yet, but I'll also update the user's cookie with the list of teams they belong too and their role. (just wont have the info of the current selected team in the cookie). This way I can also mitigate that issue without a complex query.
👍
I use a currentTeamId and currentTeamSlug in addition to teams[] - especially when logging to anlaytics