Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withMiddlewareAuthRequired does not consider Next.js baseUrl when redirecting #1741

Open
6 tasks done
Dalimil opened this issue Apr 30, 2024 · 1 comment
Open
6 tasks done

Comments

@Dalimil
Copy link

Dalimil commented Apr 30, 2024

Checklist

Description

withMiddlewareAuthRequired logic doesn't work for a custom auth0 instance with custom base URL set.

return function withMiddlewareAuthRequired(opts?): NextMiddleware {

It should be redirecting to /foo/api/auth/login/ instead of /api/auth/login when baseURL is set in auth0 config.

The issue comes from the fact that req.pathname that is used inside the function doesn't contain Nextjs base url prefix. In your code you are first checking that in the ignorePaths the login url doesn't match the pathname. And then you use the same login url to redirect. So your logic will break either during the ignorePath matching or later when you redirect (depending on whether in your config you have the base url part of the login url or not). This was already mentioned here: #1118

Reproduction

  1. create an auth0 instance with initAuth0(config) where you set the baseURL to your custom url such as localhost:3000/foo
  2. use withMiddlewareAuthRequired inside your middleware.ts
  3. Access your app, you will be redirected to /api/auth/login instead of /foo/api/auth/login/

Additional context

Already discussed here: #1118

nextjs-auth0 version

3.5.0

Next.js version

13.4.12

Node.js version

18.17.1

@Cpt0Teemo
Copy link

Running into the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants