Skip to content

v4: appBaseUrl with additional url path after domain causes /auth/callback / redirect_uri mismatch #1942

@priley86

Description

@priley86

Checklist

Description

This issue is currently encountered in v4 when you use an APP_BASE_URL that has an additional path, e.g.: APP_BASE_URL='http://localhost:3000/ai'

and attempt to use the default middleware /auth/callback route handler currently provided. In this case, you will likely encounter a Callback URL mismatch error.

Image

The current code in question in that is causing this is here:
https://github.com/auth0/nextjs-auth0/blob/main/src/server/auth-client.ts#L264

I have validated that @frederikprijck 's fix here resolves the problem.
#1941

Reproduction

  1. Provide an APP_BASE_URL like APP_BASE_URL='http://localhost:3000/ai'
  2. Set your Allowed Callback URLs to "http://localhost:3000/ai/auth/callback", and Allowed Logout URLs to "http://localhost:3000/ai"
  3. Use the default middleware setup as indicated currently in the README
  4. Attempt the login flow and note that http://localhost:3000/auth/callback is provided as the redirect_uri instead of http://localhost:3000/ai/auth/callback leading to the Callback URL mismatch above.

It does not seem to matter whether the APP_BASE_URL is provided with or without the trailing slash, e.g. "/ai" or "/ai/", the same error occurs.

An aside, but unrelated issue to this issue was also encountered when using the useUser hook currently provided here on the client:

process.env.NEXT_PUBLIC_PROFILE_ROUTE || "/auth/profile",

And seeing the /auth/profile also experience a similar issue and 404 (and not provide the /ai/auth/profile) in the request.

This issue is less urgent, b/c we can simply write our own hook to navigate around this, e.g.:

"use client";

import useSWR from "swr";

export function useUser() {
  const { data, error, isLoading } = useSWR<unknown, Error, string>(
    "/ai/auth/profile",
    (...args) =>
      fetch(...args).then((res) => {
        if (!res.ok) {
          throw new Error("Unauthorized");
        }

        return res.json();
      })
  );

  // if we have the user loaded via the provider, return it
  if (data) {
    return {
      user: data,
      isLoading: false,
      error: null,
    };
  }

  if (error) {
    return {
      user: null,
      isLoading: false,
      error,
    };
  }

  return {
    user: data,
    isLoading,
    error,
  };
}

Additional context

No response

nextjs-auth0 version

4.0.2

Next.js version

15.2.0

Node.js version

20

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