Nextjs-auth0: withPageAuthRequired returnTo path with basePath [SDK-2373]

Created on 26 Feb 2021  路  6Comments  路  Source: auth0/nextjs-auth0

Description

Using withPageAuthRequired() with the basePath configured in the next.config.js doesn't redirect the user to the expected page. HOC uses the router.asPath prop: https://github.com/auth0/nextjs-auth0/blob/main/src/frontend/with-page-auth-required.tsx#L76

Reproduction

  • Set your basePath to /app
  • Wrap a page with withPageAuthRequired
  • Go to the protected page and login
  • You should be redirected to / instead of /app

Here's a log of the router object from the withPageAuthRequired():

{
  "pathname": "/",
  "route": "/",
  "query": {},
  "asPath": "/",
  "components": {
    "/": {
      "initial": true,
      "props": {
        "pageProps": {}
      }
    },
    "/_app": {
      "styleSheets": []
    }
  },
  "isFallback": false,
  "basePath": "/app",
  "isReady": true,
  "events": {}
}

Environment

  • Version of this library used: 1.1.0
  • Version of the platform or framework used, if applicable: nextjs 10.0.7
  • Other relevant versions (language, server software, OS, browser): macOS
  • Other modules/plugins/libraries that might be involved:
bug

All 6 comments

Hi @synoptase - thanks for raising this

withPageAuthRequired should redirect you back to the page you we're trying to visit

Go to the protected page and login

What was the url of the protected page you were trying to visit when you got redirected to the login? Did it match the url of the page you were returned to after login?

When I use the withPageAuthRequired, it does not redirect me to the page I intended to visit. It redirects me to the home page always

What was the url of the protected page you were trying to visit when you got redirected to the login? Did it match the url of the page you were returned to after login?

I was trying to access the root /app of my nextjs app. The withPageAuthRequired wraps a <Home /> component, in pages/index.js. Browser is returned to / instead of /app.

Tried with another page in pages/profile.js. Browsing /app/profile logged out brings me back to /profile after logging in.

It really seems to be related to asPath (here) not containing the full url. basePath needs to be prepend.

Let me know if that's helpful enough!

That's helpful, thanks @synoptase!

@ifeanyiisitor - you seem to have a different problem, can you raise a separate issue, including steps to reproduce, what Next/Node version etc?

This is now fixed on v1.2.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lkbr picture lkbr  路  4Comments

serendipity1004 picture serendipity1004  路  3Comments

lunchboxav picture lunchboxav  路  5Comments

RyGuyM picture RyGuyM  路  5Comments

nodabladam picture nodabladam  路  7Comments