Redirect user to a dynamic route which is basically its userId or organizationId got from IdToken

I want to redirect user to a dynamic route in nextJs after the user athenticated i don’t from where it should be done like using api route or middleware.

here in “returnTo” i want the user to redirect to “/organization/[orgId]”.


// app/api/auth/[auth0]/route.js
import { handleAuth, handleLogin, handleLogout } from “@auth0/nextjs-auth0”;

export const GET = handleAuth({
login: handleLogin({
authorizationParams: {
prompt: “login”,
scope: process.env.AUTH0_SCOPE,
returnTo: “/organization”, ← - - - - - here
logout: handleLogout({
authorizationParams: {
prompt: “logout”,
returnTo: “/”,
signup: handleLogin({ authorizationParams: { screen_hint: “signup” } }),

Hi there @veshall , welcome back :raised_hand_with_fingers_splayed:

In general, we can redirect users after the login only to the Allowed Callback URLs listed in the application’s settings:

For scenarios where dynamic callbacks are desired, we suggest providing one callback URL for all those /organization/[orgId] URLs and leveraging the state parameter during authentication.

Here, the concept is explained generically - How do I set up a dynamic Allowed Callback URL?

I also found the topic where the getLoginState is used to redirect the user to the origin/referer URL but you may try to go analogically with your use case - Redirecting back to starting page dynamically with nextjs-auth0

Here is the library reference: LoginOptions | @auth0/nextjs-auth0

Feel free to try this out, and please let us know if you have any follow-up questions along the way.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.