# How to handle access decline gracefully with @auth0/nextjs-auth0?

**URL:** <https://community.auth0.com/t/how-to-handle-access-decline-gracefully-with-auth0-nextjs-auth0/86718>\
**Category:** Get Help\
**Created:** [July 11, 2022, 4:57am UTC](https://community.auth0.com/t/how-to-handle-access-decline-gracefully-with-auth0-nextjs-auth0/86718 "2022-07-11T04:57:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mishchief](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mishchief/32/38673_2.png) [@mishchief](https://community.auth0.com/u/mishchief)\
**Post date:** [July 11, 2022, 4:57am UTC](https://community.auth0.com/t/how-to-handle-access-decline-gracefully-with-auth0-nextjs-auth0/86718/1 "2022-07-11T04:57:52Z")

</div>

Hello everyone,

I have setup the auth0 handler in my NextJS app according to [GitHub - auth0/nextjs-auth0: Next.js SDK for signing in with Auth0](https://github.com/auth0/nextjs-auth0)

The thing that is bothering me is that if a user declines to authorize the app on Auth0, they get bounced back to my /callback endpoint which is wrapped with `withPageAuthRequired(Component)` which then errors out the following

```auto
Server Error

BadRequestError: access_denied (User did not authorize the request)

<small>This error happened while generating the page. Any console logs will be displayed in the terminal window.</small>

```

So I wonder if there’s anything I can do so that I can handle this more gracefully? Do I need to remove the `withPageAuthRequired(Component)` wrapper? I would rather keep it so that route is only available for signed in users.

Is redirecting them back to the root route if they chose to deny access a possibility?

Steps to reproduce:

- Have a /callback route that is wrapped with `withPageAuthRequired()` function which comes with the @auth0/nextjs-auth0 library
- Log in to an account with your tenant and make sure you’re asking for: 1.Profile access and 2. Allow offline access
- Click Decline
- Get redirected to the /callback route

Thanks!

---

<div class="post-metadata">

**Author:** ![mishchief](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mishchief/32/38673_2.png) [@mishchief](https://community.auth0.com/u/mishchief)\
**Post date:** [July 11, 2022, 3:34pm UTC](https://community.auth0.com/t/how-to-handle-access-decline-gracefully-with-auth0-nextjs-auth0/86718/2 "2022-07-11T15:34:05Z")

</div>

Ha! Fixed it!

For anyone interested:

In your `[...auth0].js` file you need to add custom callback handling like so:

```auto
const authOptions = {
  async callback(req, res) {
    try {
      await handleCallback(req, res, { afterCallback })
    } catch (error) {
      console.log(error)
      res.status(error.status || 500).end(error.message)
    }
  },
}

export default handleAuth(authOptions)

```

The magic sauce is happening in the catch block where it will handle the denial of service correctly. For me I will handle it by login out the user and sending it to the root route.

Thanks to anyone that looked at this!

---

<div class="post-metadata">

**Author:** ![lihua.zhang](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lihua.zhang/32/39683_2.png) [@lihua.zhang](https://community.auth0.com/u/lihua.zhang)\
**Post date:** [July 11, 2022, 6:22pm UTC](https://community.auth0.com/t/how-to-handle-access-decline-gracefully-with-auth0-nextjs-auth0/86718/3 "2022-07-11T18:22:52Z")

</div>

Thank you @mishchief for sharing your solution with us! 👍 👏

---

<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:** [July 28, 2022, 1:35pm UTC](https://community.auth0.com/t/how-to-handle-access-decline-gracefully-with-auth0-nextjs-auth0/86718/4 "2022-07-28T13:35:05Z")

</div>


