Including multi-tenant config into tRPC context
Hey all,
I've been working on upgrading my app to support multi-tenancy, inspired by Vercel's Platforms starter kit.
The core of the relevant logic comes down to this Next.js middleware:
This middleware is set up to explicitly exclude requests to
/api
Now, within my tRPC router, it is helpful in certain places to be able to access the tenantSlug
value. I could go and change all my tRPC queries/mutations to take that as input and change client-side code everywhere but that would be hundreds of functions and even more calls in the client code.
I have been thinking about how to get this be part of tRPC's context, but I'm a bit stuck
Currently my context looks like this (i have a mix of HTTP and WebSockets)
Since the middleware ignores requests to /api
- the req
object in context (in case of HTTP calls) doesn't include the subdomain/slug in the rquest headers. I figured someone here had to have run into this in the past and may have figured out a solutin already - so any help is appreciated!Solution:Jump to solution
For anyone who stumbles upon this later, my fix for now is to do this:
```ts
export async function createContext(
ctx: CreateWSSContextFnOptions | CreateNextContextOptions,...
3 Replies
Solution
For anyone who stumbles upon this later, my fix for now is to do this:
why not just use middleware for page, check domain for tenant in create context?
@haardik | LearnWeb3 Hi~~~
- Could you please share the details of
getAuthOptions()
?
- If not, could you please tell me if it's not much different from using the regular AuthOptions
in the next-auth?