Skip to content

Remove the requirement to configure the AUTH0_BASE_URL #1330

Closed
@RobertAron

Description

@RobertAron

Checklist

  • I have looked into the Readme, Examples, and FAQ and have not found a suitable solution or answer.
  • I have looked into the API documentation and have not found a suitable solution or answer.
  • I have searched the issues and have not found a suitable solution or answer.
  • I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • I agree to the terms within the Auth0 Code of Conduct.

Describe the problem you'd like to have solved

Requiring setting the AUTH0_BASE_URL complicates deployment. As far as I can tell it seems unnecessary.

Describe the ideal solution

Instead of having the AUTH0_BASE_URL be a required environment variable, it could be optional. In cases where it's not supplied, the login behavior will log back wherever the initial request came from.

Alternatives and current workarounds

import {
  handleAuth,
  handleCallback,
  handleLogin,
  handleLogout,
} from "@auth0/nextjs-auth0";

function getRedirectUrls(webUrl: string | undefined) {
  if (webUrl === undefined)
    throw new Error("Error get base Url. Missing request URL.");
  const urlObject = new URL(webUrl);
  const returnTo = `${urlObject.protocol}//${urlObject.host}`;
  return {
    returnTo,
    redirect_uri: `${returnTo}/api/auth/callback`,
  };
}

export const GET = handleAuth({
  // @ts-ignore
  callback: (req, res) => {
    const { redirect_uri } = getRedirectUrls(req.url);
    return handleCallback(req, res, {
      authorizationParams: {
        audience: "https://my-audience.us.auth0.com/api/v2/",
        scope: "openid profile email offline_access",
        redirect_uri: redirect_uri,
      },
      redirectUri: redirect_uri,
    });
  },
  // @ts-ignore
  login: (req, res) => {
    const { returnTo, redirect_uri } = getRedirectUrls(req.url);
    return handleLogin({
      authorizationParams: {
        audience: "https://my-audience.us.auth0.com/api/v2/",
        scope: "openid profile email offline_access",
        redirect_uri,
      },
      returnTo,
    })(req, res);
  },
  // @ts-ignore
  logout: (req, res) => {
    return handleLogout({
      returnTo: getRedirectUrls(req.url).returnTo,
    })(req, res);
  },
});

Additional context

Here are some other links that I think are relevant.

#298 This makes it seem like it's an OK solution for this to be dynamic
It's even suggested here: https://community.auth0.com/t/configure-multiple-domains-with-nextjs/107002

Overriding during runtime seems ok #552

It would remove the need for special configuration for deployments like vercel. #383

As far as I can it would be completely safe for this behavior since auth0 configuration requires that you list the allowed callback URLs on the auth0 dashboard.

Thanks for all your hard work <3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions