I am currently developing a NextJS application that functions as a multi-tenant SaaS platform.
Within the app, each customer has the ability to either utilize a subdomain on our site or map their own custom domain via CNAME.
My goal is to enable our customers to permit their employees to log in on their designated subdomain or custom domain.
export const authOptions: NextAuthOptions = {
// Configure one or more authentication providers
providers: [
EMAIL PROVIDER
// ...add more providers here
],
pages: {
signIn: `/login`,
verifyRequest: `/verify`,
},
adapter: PrismaAdapter(prisma),
callbacks: {
},
cookies: {
sessionToken: {
name: 'next-auth.session-token',
options: {
httpOnly: true,
sameSite: 'lax',
path: '/',
domain: process.env.NODE_ENV === 'production' ? '.mysaas.com' : undefined,
secure: process.env.NODE_ENV && process.env.NODE_ENV === 'production' ? true : false
}
},
callbackUrl: {
name: 'next-auth.callback-url',
options: {
sameSite: 'lax',
path: '/',
domain: process.env.NODE_ENV === 'production' ? '.mysaas.com' : undefined,
secure: process.env.NODE_ENV && process.env.NODE_ENV === 'production' ? true : false
}
},
csrfToken: {
name: 'next-auth.csrf-token',
options: {
sameSite: 'lax',
path: '/',
domain: process.env.NODE_ENV === 'production' ? '.mysaas.com' : undefined,
secure: process.env.NODE_ENV && process.env.NODE_ENV === 'production' ? true : false
}
}
}
}
export default NextAuth(authOptions)
The existing setup of using '.mysaas.com' for the domain cookie allows for functionality with subdomains successfully.
However, there is an issue when it comes to making it work seamlessly with a custom domain mapped to a subdomain. How can this be resolved?
If it were possible to dynamically set the cookie domain based on the actual domain visited, such as changing from .mysaas.com
to .mycustomdomain.com
when accessed from the custom domain, then the problem would be solved.
Unfortunately, I have not been able to find a way to achieve setting the cookie domain dynamically. Any suggestions or assistance would be greatly appreciated.