# How can I override the callback handler's redirect?

**URL:** <https://community.auth0.com/t/how-can-i-override-the-callback-handlers-redirect/89285>\
**Category:** Get Help\
**Tags:** nextjs\
**Created:** [August 23, 2022, 5:37pm UTC](https://community.auth0.com/t/how-can-i-override-the-callback-handlers-redirect/89285 "2022-08-23T17:37:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelschufi](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/michaelschufi/32/39495_2.png) [@michaelschufi](https://community.auth0.com/u/michaelschufi)\
**Post date:** [August 23, 2022, 5:37pm UTC](https://community.auth0.com/t/how-can-i-override-the-callback-handlers-redirect/89285/1 "2022-08-23T17:37:58Z")

</div>

Hi

I’m trying to redirect the user to his previous page. To do that, I set a cookie before the login handler runs and read the cookie in the callback handler’s `afterCallback`. (see code below)

However, I always get redirected to the root of the page.

Note: `nookies.destroy({ res: res2 }, 'authReturnUrl');` doesn’t work as well. It seems like the `handleCallback` blocks altering the response.

So, how can I redirect the user properly?

Thank you in advance, any help would be appreciated. 🙂

### Code

```auto
import { handleAuth, handleCallback, handleLogin } from '@auth0/nextjs-auth0';
import nookies from 'nookies';

export default handleAuth({
  login(req, res) {
    const returnUrl = req.query.returnUrl;

    if (returnUrl) {
      nookies.set({ res }, 'authReturnUrl', returnUrl, {
        maxAge: 3600,
        path: '/',
      });
    }

    return handleLogin(req, res);
  },
  callback(req, res) {
    const { authReturnUrl } = nookies.get({ req });

    const returnUrl = authReturnUrl || '/';

    return handleCallback(req, res, {
      afterCallback(_, res2, session) {
        res2.setHeader('Location', returnUrl);
        res2.status(307);

        nookies.destroy({ res: res2 }, 'authReturnUrl');

        // res2.redirect(returnUrl);
        // ^ doesn't work either

        return session;
      },
    });
  },
});

```

### Versions

- SDK: @auth0/nextjs-auth0
- SDK Version: 1.9.1
- Next.js Version: 12.2.5

---

<div class="post-metadata">

**Author:** ![michaelschufi](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/michaelschufi/32/39495_2.png) [@michaelschufi](https://community.auth0.com/u/michaelschufi)\
**Post date:** [August 24, 2022, 11:16am UTC](https://community.auth0.com/t/how-can-i-override-the-callback-handlers-redirect/89285/2 "2022-08-24T11:16:57Z")

</div>

In the meantime, I’ve figured it out. There’s actually a handler option I’ve overlooked.

My current, working approach:

```javascript
const getLoginState = (req: NextApiRequest, loginOptions: LoginOptions) => {
  const returnTo = req.query.returnTo;

  return { ...loginOptions, returnTo };
};

export default withSentry(
  handleAuth({
    login(req, res) {
      return handleLogin(req, res, { getLoginState });
    },
    logout(req, res, options) {
      const returnTo = req.query.returnTo;

      return handleLogout(req, res, { ...options, returnTo });
    },
  }),
);

```

---

<div class="post-metadata">

**Author:** ![andrew.s1](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andrew.s1/32/33193_2.png) [@andrew.s1](https://community.auth0.com/u/andrew.s1)\
**Post date:** [December 7, 2022, 7:34pm UTC](https://community.auth0.com/t/how-can-i-override-the-callback-handlers-redirect/89285/3 "2022-12-07T19:34:43Z")

</div>

@michaelschufi I’m glad you solved it.

I’m here trying to implement you approach, and I"m not making any headway.  
Even the `returnTo` variable in `logout` is always returning `null`. Is there something that is missing from your post that could be helpful, maybe the `middleware` file.
