Using next-auth in a multi-tenant application.
Unanswered
Exzotic posted this in #help-forum
ExzoticOP
Hello! Im wondering if its possible to use next-auth to provide auth for the multi tenant sites?
38 Replies
Satin Angora
The short answer is yes. Its is very possible to use next-auth to provide auth for muiltiple sites.
https://next-auth.js.org/configuration/options
If you use something like turborepo for instance and create a package to handle authentication that could potentially be a good option for you but perhaps if you give a bit more specifics we can come up with a more niche answer
https://next-auth.js.org/configuration/options
If you use something like turborepo for instance and create a package to handle authentication that could potentially be a good option for you but perhaps if you give a bit more specifics we can come up with a more niche answer
@Satin Angora The short answer is yes. Its is very possible to use next-auth to provide auth for muiltiple sites.
https://next-auth.js.org/configuration/options
If you use something like turborepo for instance and create a package to handle authentication that could potentially be a good option for you but perhaps if you give a bit more specifics we can come up with a more niche answer
ExzoticOP
Ah right, will try explain it in more detail. I have next auth setup normally so that on for example
app.mydomain.com you can login, there you can create your site (like the platforms example). Though I want those sites to also have the ability to have an account in (just using github or discord provider).but i dont see an easy way of doing it as the sites could be under a subdomain like
site1.mydomain.com or their custom domain sitedomain.comSatin Angora
So to be clear you want them to share the same DB/session for auth? Or is it that you want to use nextauth for two applications ?
Because realistically it should be as easy as changing the NEXTAUTH_URL in theory in the env
@Satin Angora So to be clear you want them to share the same DB/session for auth? Or is it that you want to use nextauth for two applications ?
ExzoticOP
Its not a big deal at the moment between them, would just like to see if I can get it up and working for now
Satin Angora
May I ask whats your full tech stack?
Are you using a monolith structure?
ExzoticOP
Yeah just nextjs
Similiar to https://github.com/vercel/platforms/tree/main
Been using that as an example / base
Satin Angora
So you have two applications app.yourdomain.com
app.2.yourdomain.com
You'd like to be able to use nextAuth on both applications and they don't need to share the same session?
app.2.yourdomain.com
You'd like to be able to use nextAuth on both applications and they don't need to share the same session?
In this case I would just implement next-auth separately for both apps.
Or if you're going off of that example where tehy have the [domain] you can dynamically set the next auth url in the environment or programatically to redirect to auth but it really boils down to the functionality of the application itself in terms of the "best" way to go about it
Or if you're going off of that example where tehy have the [domain] you can dynamically set the next auth url in the environment or programatically to redirect to auth but it really boils down to the functionality of the application itself in terms of the "best" way to go about it
As people can create many sites under different subdomains in this app
Satin Angora
Ok yeah thats what I figured when you linked that above as a template
You should be able to get the sessions from the root domain /session route between the tenants
https://next-auth.js.org/getting-started/rest-api
https://next-auth.js.org/getting-started/rest-api
Frankly, I haven't really played around with next-auth in this context but the way I would do it off the cuff is utalize the next-auth endpoints to verify their sessions and allow them to sign in from the main authentication point which would be for example app.mydomain.com and then redirect them to the other "domain"
ExzoticOP
hmm but I dont see how the sessions will share across?
Satin Angora
Well the session would be saved in the db. Or you can set it up that way
session: {
// Choose how you want to save the user session.
// The default is `"jwt"`, an encrypted JWT (JWE) stored in the session cookie.
// If you use an `adapter` however, we default it to `"database"` instead.
// You can still force a JWT session by explicitly defining `"jwt"`.
// When using `"database"`, the session cookie will only contain a `sessionToken` value,
// which is used to look up the session in the database.
strategy: "database",
// Seconds - How long until an idle session expires and is no longer valid.
maxAge: 30 * 24 * 60 * 60, // 30 days
// Seconds - Throttle how frequently to write to database to extend a session.
// Use it to limit write operations. Set to 0 to always update the database.
// Note: This option is ignored if using JSON Web Tokens
updateAge: 24 * 60 * 60, // 24 hours
// The session token is usually either a random UUID or string, however if you
// need a more customized session token string, you can define your own generate function.
generateSessionToken: () => {
return randomUUID?.() ?? randomBytes(32).toString("hex")
}
}Specifically
ExzoticOP
But how would the session token cookie be set the same between each subdomain?
just came across this, will try see what this does
Satin Angora
There are docs on this with next-auth one moment with how to set it to be cross-domain cookies just finding it quickly haha
ExzoticOP
Will take a look, thanks
Satin Angora
options: {
httpOnly: true,
sameSite: 'Lax',
path: '/',
secure: true, // depending on if your prod or not
domain: '.mydomain.com', // Allows the cookie to be shared across subdomains
},
That should allow for shared sessions between subdomains
ExzoticOP
Right
When logging in on the root domain, it doesnt seem to share between subdomains but I shall try a different way
ExzoticOP
I guess this would also have an issue if people use a custom domain
ExzoticOP
I had a scuffed solution that almost worked with discord oauth, but ran into an issue that doesn’t seem fixable.
But basically called the “signIn†method as default but didn’t redirect to the link, instead replace the redirect uri to “auth.mydomain.com/redirect?siteId=abc†which set a cookie to redirect the auth.mydomain.com discord callback, back to the right domain to next-auths callback
But basically called the “signIn†method as default but didn’t redirect to the link, instead replace the redirect uri to “auth.mydomain.com/redirect?siteId=abc†which set a cookie to redirect the auth.mydomain.com discord callback, back to the right domain to next-auths callback
But it seems the redirect uri is also sent in the token grant request so it just gets “invalid redirect uriâ€
ExzoticOP
Unless I’m able to modify the package to change the redirect url based on the domain the request is to in the callback
ExzoticOP
Ended up just doing next-auth on the main app then custom jwt auth for tenant sites. If anyone has a better idea, feel free to send