# onRedirectCallback receives undefined in nexts production build static generation

**URL:** <https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873>\
**Category:** Get Help\
**Tags:** react-sdk, refresh, redirect\
**Created:** [April 27, 2021, 9:16pm UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873 "2021-04-27T21:16:44Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![aajmera](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/aajmera/32/14308_2.png) [@aajmera](https://community.auth0.com/u/aajmera)\
**Post date:** [April 27, 2021, 9:16pm UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873/1 "2021-04-27T21:16:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adam.mcgrath](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/adam.mcgrath/32/22033_2.png) [@adam.mcgrath](https://community.auth0.com/u/adam.mcgrath)\
**Post date:** [April 29, 2021, 8:04am UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873/2 "2021-04-29T08:04:08Z")

</div>

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](https://github.com/auth0/auth0-react/blob/master/src/auth0-provider.tsx#L256)

And make sure it is sending your expected `appState`

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [April 29, 2021, 8:39am UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873/3 "2021-04-29T08:39:44Z")

</div>

Thanks for helping on this one Adam!

---

<div class="post-metadata">

**Author:** ![aajmera](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/aajmera/32/14308_2.png) [@aajmera](https://community.auth0.com/u/aajmera)\
**Post date:** [April 29, 2021, 9:13am UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873/4 "2021-04-29T09:13:11Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![adam.mcgrath](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/adam.mcgrath/32/22033_2.png) [@adam.mcgrath](https://community.auth0.com/u/adam.mcgrath)\
**Post date:** [April 29, 2021, 9:21am UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873/5 "2021-04-29T09:21:33Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![aajmera](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/aajmera/32/14308_2.png) [@aajmera](https://community.auth0.com/u/aajmera)\
**Post date:** [April 29, 2021, 2:04pm UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873/6 "2021-04-29T14:04:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adam.mcgrath](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/adam.mcgrath/32/22033_2.png) [@adam.mcgrath](https://community.auth0.com/u/adam.mcgrath)\
**Post date:** [April 30, 2021, 9:48am UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873/7 "2021-04-30T09:48:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aajmera](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/aajmera/32/14308_2.png) [@aajmera](https://community.auth0.com/u/aajmera)\
**Post date:** [May 12, 2021, 3:24pm UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873/8 "2021-05-12T15:24:29Z")

</div>

@adam.mcgrath  
Sorry for the delay:

> **[GitHub - ajmeraaxesh/simple-auth0: A simple example of next's and auth0](https://github.com/ajmeraaxesh/simple-auth0)**
>
> A simple example of next's and auth0. Contribute to ajmeraaxesh/simple-auth0 development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![adam.mcgrath](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/adam.mcgrath/32/22033_2.png) [@adam.mcgrath](https://community.auth0.com/u/adam.mcgrath)\
**Post date:** [May 13, 2021, 1:44pm UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873/9 "2021-05-13T13:44:22Z")

</div>

Hi @aajmera - thanks for sharing that app

When I run it in development mode, visit `http://localhost:3000` and am prompted to login - the `appState` passed to `onRedirectCallback` is `undefined`. The console logs:

`OnRedirectCallback()::: undefined origin: http://localhost:3000`

This is because you need to pass your `appState` in the `appState` property to `loginWithRedirect` here [simple-auth0/UnAuthenticatedApp.js at master · ajmeraaxesh/simple-auth0 · GitHub](https://github.com/ajmeraaxesh/simple-auth0/blob/master/src/pages/UnAuthenticatedApp.js#L8-L10)

Change:

```plaintext
loginWithRedirect({
  portalType: 'Partner Portal',
})

```

To:

```plaintext
loginWithRedirect({
  appState: { portalType: 'Partner Portal' }
})

```

When I do this, I see the `appState` reflected in the console logs when I login

```auto
OnRedirectCallback()::: {portalType: "Partner Portal"} 

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [May 13, 2021, 1:54pm UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873/10 "2021-05-13T13:54:08Z")

</div>

Thanks for following up on that Adam!

---

<div class="post-metadata">

**Author:** ![aajmera](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/aajmera/32/14308_2.png) [@aajmera](https://community.auth0.com/u/aajmera)\
**Post date:** [May 14, 2021, 6:45am UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873/11 "2021-05-14T06:45:51Z")

</div>

@adam.mcgrath The issue is not about the options being passed to loginWIthRedirect.

Its about doing a hard refresh on "“[http://localhost:3000/user](http://localhost:3000/user)” and making sure that we are redirected to the same page  
[simple-auth0/user.js at master · ajmeraaxesh/simple-auth0 · GitHub](https://github.com/ajmeraaxesh/simple-auth0/blob/master/src/pages/user.js)

[https://auth0.github.io/auth0-react/modules/with\_authentication\_required.html](https://auth0.github.io/auth0-react/modules/with_authentication_required.html)

[https://auth0.github.io/auth0-react/interfaces/with\_authentication\_required.withauthenticationrequiredoptions.html](https://auth0.github.io/auth0-react/interfaces/with_authentication_required.withauthenticationrequiredoptions.html)

If there is still an issue. I am happy to talk to you

---

<div class="post-metadata">

**Author:** ![adam.mcgrath](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/adam.mcgrath/32/22033_2.png) [@adam.mcgrath](https://community.auth0.com/u/adam.mcgrath)\
**Post date:** [May 14, 2021, 10:42am UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873/12 "2021-05-14T10:42:18Z")

</div>

Hi @aajmera - if I understand you correctly, the steps to reproduce your issue are:

1. Run the application
2. visit `http://localhost:3000/user` with no cookies
3. Login and return to the application
4. expect to be returned to `/user` - actually returned to `/authenticated`

If this is the case, then this is casued by [simple-auth0/index.js at master · ajmeraaxesh/simple-auth0 · GitHub](https://github.com/ajmeraaxesh/simple-auth0/blob/master/src/pages/index.js#L27)

When you return to the application after login, you are returned to the home page to process the state and redirect you to your `state.returnTo` (if specified). Since you have a `router.push('/authenticated')` in your home page’s render that happens after authentication - the user is redirected to `/user` as expected, then the code in the the homepage overrides this and redierects the user to `authenticated`.

I commented out the `router.push('/authenticated')` in [simple-auth0/index.js at master · ajmeraaxesh/simple-auth0 · GitHub](https://github.com/ajmeraaxesh/simple-auth0/blob/master/src/pages/index.js#L27) and the application retuned me to `/user` after login as expected

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [May 27, 2021, 11:48am UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873/13 "2021-05-27T11:48:30Z")

</div>

Thanks once again for helping on this one Adam!

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [May 27, 2021, 11:48am UTC](https://community.auth0.com/t/onredirectcallback-receives-undefined-in-nexts-production-build-static-generation/61873/14 "2021-05-27T11:48:34Z")

</div>


