onRedirectCallback receives undefined in nexts production build static generation

Hi,

I am using auth-react sdk for my Nextjs Static site. The scenarios is as follows. Once the user is logged in and shown a particular protected route page.

say something like https:/example.com/dashboard/users.

Since this is a protected route. I am using the withAuthentiationRequired (Higher order component). The code looks something like this

export default withAuthenticationRequired(
  UsersList,
 {
    onRedirecting: () => <AuthenticationLoading />,
    returnTo: authReturnToUrl,
    loginOptions: {
      portalType: ''Some portal name',
      appState: {
        foo: 'bar',
      },
    },
  }
)

Now this is what my AuthProvider looks like

 <Auth0Provider
      domain={process.env.NEXT_PUBLIC_AUTH0_DOMAIN}
      clientId={process.env.NEXT_PUBLIC_CLIENT_ID}
      audience={process.env.NEXT_PUBLIC_AUTH0_AUDIENCE}
      redirectUri={typeof window !== 'undefined' && window.location.origin}
      onRedirectCallback={onRedirectCallback}
      useRefreshTokens={true}
    >

const onRedirectCallback = appState => {
  console.log('OnRedirectCallback()::: ', appState)
  Router.replace(appState?.returnTo || window.location.origin)
}

now in the development I see the following

nRedirectCallback()::: {foo: “bar”, returnTo: “/dashboard/users”}

But in production. Which is static build of next’s. I see the following:
onRedirectCallback()::: undefined

My idea is that when a user refreshes “/dashboard/users” page. Even though authentication happens, the value passed to onRedirectCallback is completed ignored.

Please help

Hi @aajmera - I can’t see anything wrong with your code.

I can only recommend that you try debugging your call to loginWithRedirect here auth0-react/auth0-provider.tsx at master · auth0/auth0-react · GitHub

And make sure it is sending your expected appState

1 Like

Thanks for helping on this one Adam!

I am still not getting it. This is not about calling loginWithRedirect. It is about calling withAuthenticationRequired (Higher Order Component).

If you allow me. I can send you a very simple app to demo this issue and the problem is. I am seeing this issue only in the production build.

While working in dev environment. the values are populated correctly.

Please note: When I say production build, I am referring to generating a static site build using the commands “next build” and “next export”.

If you allow me. I can send you a very simple app to demo this issue and the problem is.

Sure, please do - thanks @aajmera

1 Like

@adam.mcgrath Please suggest how do I send you the project. I cannot upload the zip file here

@aajmera - if it’s just a simple app to demo the issue, can you put it on GitHub?

1 Like