vercel / platforms

A full-stack Next.js app with multi-tenancy and custom domain support. Built with Next.js App Router and the Vercel Domains API.
https://app.vercel.pub
5.38k stars 689 forks source link

Config per Tenant #285

Closed josh9060 closed 7 months ago

josh9060 commented 11 months ago

Hi all,

We have a different use case concerning authentication. Each Tenant would be set up manually for our product, and thus we would configure them.

Specifically, in regards to authentication, we use ORY Kratos. Each of our Tenants get their own ORY Kratos instance. This is due to the design of Kratos. The problem here is that each Tenant has their own login redirect URL, secret keys, etc.

The question is, how would one go about solving this issue?

I will give you our current outlook.

We plan on storing the configuration in a key store. When a Tenant visits [tenant].app.company.com we would get their Tenant ID from the URL, and fetch the configuration behind the scenes for that Tenant injecting it into each page (the non-sensitive values). We would also fetch this configuration on certain API endpoints (the sensitive values).

Now, I can see some issues with this approach. 1. Caching this sensitive data isn't going to be easy, and will result in a call every time. 2. The configuration would be available at the page level (we're using old routing right now) and thus each hook, etc. Therefore, for example, a RQ hook would need to accept, for example the Tenant ORY Kratos redirect URL as a parameter. This would mean page level configuration would be passed around in a, potentially unmaintainable way. 3. We have API routes that need this sensitive configuration, and therefore, every time an API endpoint is called we would have to request the config again!

There are plenty of issues with the approach I have outlined above.

So, has anyone seen a maintainable approach to this problem? Any solutions for the above issue? Any ideas would be appreciated.

steven-tey commented 7 months ago

Sorry for the late reply here! This is a great question – we've had folks evaluate Platforms with Auth solutions like ORY / Clerk / Auth0 before. Short answer is yes it should work, but you'll probably need to do this in the middleware.

I'm not very familiar with ORY so I'm going to cc some folks from their team for help: @tacurran @aeneasr

Will also move this to a discussion!