Customize your redirect URLs
To avoid breaking a user's flow through your app, when a user navigates to a Clerk sign up or sign in page via a link or button, Clerk will:
- Persist the previous page's URL in a
redirect_url
query string - Navigate back to that page after the sign-up or sign-in is completed
For example, a user selecting a sign-in button on example.com/foo
will navigate to example.com/sign-in?redirect_url=example.com/foo
, then navigate back to example.com/foo
upon completing the sign-in process.
However, you can customize this behavior to redirect users to a specific page by using the following methods:
Environment variables
It is recommended to use environment variables to define your redirect URLs.
Fallback redirect
You can define a fallback redirect URL in the case that there is no redirect_url
in the path already. This is useful for users who navigate directly to the sign-in or sign-up page.
The following example shows how to define a fallback redirect URL for both sign-in and sign-up pages:
Force redirect
If you would like to override the redirect_url
value and supply a custom redirect URL after sign-in or sign-up, you can use the following environment variables:
Middleware
If you are using Next.js and want a more programmatically generated redirect option, you can use the auth.protect()
method in your Clerk middleware.
Redirect URL props
You can explicitly define the redirect paths after sign-up or sign-in by using the properties described in this section with Clerk's components. In general, it is recommended to use environment variables instead.
Fallback redirect URL props
The "fallback redirect URL" props will only be used if there is no redirect_url
value. This can happen if the user has navigated directly to the sign up or sign in page.
fallbackRedirectUrl
- Used by sign-in and sign-up related components.signInFallbackRedirectUrl
- Used on sign up components, such as<SignUp />
and<SignUpButton>
.signUpFallbackRedirectUrl
- Used on sign in components, such as<SignIn />
and<SignInButton>
.
Force redirect URL props
The "force redirect URL" props will always redirect to the provided url after sign up or sign in, regardless of what page the user was on before, and will override the redirect_url
value if present.
forceRedirectUrl
- Used by sign-in and sign-up related components.signInForceRedirectUrl
- Used on sign up components, such as<SignUp />
and<SignUpButton>
.signUpForceRedirectUrl
- Used on sign in components, such as<SignIn />
and<SignInButton>
.
Control components
Button components
<SignInButton>
and <SignUpButton>
also accepts these properties.
Feedback
Last updated on