onRedirectCallback receives undefined in nexts production build static generation


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(
    onRedirecting: () => <AuthenticationLoading />,
    returnTo: authReturnToUrl,
    loginOptions: {
      portalType: ''Some portal name',
      appState: {
        foo: 'bar',

Now this is what my AuthProvider looks like

      redirectUri={typeof window !== 'undefined' && window.location.origin}

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