Need help on creating a dynamic callback url

Hi, I am using NextJS project both running locally and on the cloud. I would like to have 2 separate callback URLs, so that I can choose to callback to localhost if NODE_ENV is development and to callback to an https url when NODE_ENV is production. Is there a way to do that for NextJS, or do I have to use 2 separate Auth0 applications?

Hi @slcredentialmanager,

Welcome to the Auth0 Community!

First, let me clarify that the redirect_uri parameter defines the destination after a user successfully logs in, which must match one of the URLs defined in the Allowed Callback URLs of your Application settings.

Given that, I recommend adding both Callback URLs to the list of Allowed Callback URLs for that app. Then to reach the different Callback URLs, you can specify them in the redirect_uri.

For example:

//Localhost
https://YOUR_DOMAIN/authorize?
    response_type=code&
    client_id=YOUR_CLIENT_ID&
    redirect_uri=http://localhost:3000/callback&
    scope={scope}&
    state={state}
//Cloud
https://YOUR_DOMAIN/authorize?
    response_type=code&
    client_id=YOUR_CLIENT_ID&
    redirect_uri=https://myexampleapp.com/callback&
    scope={scope}&
    state={state}

Reference: Redirect Users

I hope this helps!

Please let me know if there’s anything else I can do to help.

Thanks,

Rueben

Thanks, but my question relates more-so to how I would do this in NextJS, as there is little documentation on the handleAuth() API

Hi @slcredentialmanager,

Thank you for your response.

In the Next.JS SDK, you can pass a returnTo property to the handleLogin() handler of your preferred Callback URL. See below:

export default handleAuth({
  login: handleLogin({
    returnTo: 'YOUR_REDIRECT_URI' // Pass in your preferred callback URL
  })
});

Reference: LoginOptions | @auth0/nextjs-auth0

Please let me know how this goes for you.

Thanks,
Rueben

Thanks @rueben.tiow. I actually ended up just changing the AUTH0_BASE_URL in production to point to my site URL. That ended up working

1 Like

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