Authentication across different domains
Clerk supports sharing sessions across different domains by adding one or many satellite domains to an application.
Your "primary" domain is where the authentication state lives, and satellite domains are able to securely read that state from the primary domain, enabling a seamless authentication flow across domains.
Users must complete both the sign-in and sign-up flows on the primary domain by using the <SignIn />
component or useSignIn()
hook for sign-in and <SignUp />
component or useSignUp()
hook for sign-up.
To access authentication state from a satellite domain, users will be transparently redirected to the primary domain. If users need to sign in, they must be redirected to a sign in flow hosted on the primary domain, then redirected back to the originating satellite domain. The same redirection process applies to sign-up flows.
How to add satellite domains
To get started, you need to create an application from the Clerk Dashboard. Once you create an instance via the Clerk Dashboard, you will be prompted to choose a domain. This is your primary domain. For the purposes of this guide:
- In production, the primary domain will be
primary.dev
- In development, the primary domain will be
localhost:3000
.
When building your sign-in flow, you must configure it to run within your primary application, e.g. on /sign-in
.
Add your first satellite domain
To add a satellite domain:
- In the Clerk Dashboard, navigate to the Domains page.
- Select the Satellites tab.
- Select the Add satellite domain button and follow the instructions provided.
For the purposes of this guide:
- In production, the satellite domain will be
satellite.dev
. - In development, the satellite domain will be
localhost:3001
.
Configure your satellite app
There are two ways that you can configure your Clerk satellite application to work with the primary domain:
- Using environment variables
- Using properties
Use the following tabs to select your preferred method. Clerk recommends using environment variables.
You can configure your satellite application by setting the following environment variables:
-
In the
.env
file associated with your primary domain:.env NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=YOUR_PUBLISHABLE_KEY CLERK_SECRET_KEY=YOUR_SECRET_KEY NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
-
In the
.env
file associated with your other (satellite) domain:.env NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=YOUR_PUBLISHABLE_KEY CLERK_SECRET_KEY=YOUR_SECRET_KEY NEXT_PUBLIC_CLERK_IS_SATELLITE=true # Production example: NEXT_PUBLIC_CLERK_DOMAIN=satellite.dev NEXT_PUBLIC_CLERK_SIGN_IN_URL=https://primary.dev/sign-in NEXT_PUBLIC_CLERK_SIGN_UP_URL=https://primary.dev/sign-up # Development example: # NEXT_PUBLIC_CLERK_DOMAIN=http://localhost:3001 # NEXT_PUBLIC_CLERK_SIGN_IN_URL=http://localhost:3000/sign-in # NEXT_PUBLIC_CLERK_SIGN_UP_URL=http://localhost:3000/sign-up
-
You will also need to add the
allowedRedirectOrigins
property to<ClerkProvider>
on your primary domain app to ensure that the redirect back from primary to satellite domain works correctly. For example:app /layout.tsx import { ClerkProvider } from '@clerk/nextjs' export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <html lang="en"> <body> <ClerkProvider allowedRedirectOrigins={['http://localhost:3001']}>{children}</ClerkProvider> </body> </html> ) }
Ready to go 🎉
Your satellite application should now be able to access the authentication state from your satellite domain!
You can see it in action by:
- Visiting the primary domain and signing in.
- Visiting the satellite domain.
- You now have an active session in the satellite domain, so you can see the
<UserProfile />
component and update your information.
You can repeat this process and create as many satellite applications as you need.
If you have any questions about satellite domains, or you're having any trouble setting this up, contact
Feedback
Last updated on