# NextJS13 + Auth0 - Redirect user based on certain conditions in callback method

**URL:** <https://community.auth0.com/t/nextjs13-auth0-redirect-user-based-on-certain-conditions-in-callback-method/116987>\
**Category:** Get Help\
**Tags:** login-experience, new-universal-login-experience\
**Created:** [September 23, 2023, 10:11am UTC](https://community.auth0.com/t/nextjs13-auth0-redirect-user-based-on-certain-conditions-in-callback-method/116987 "2023-09-23T10:11:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![codenameredpanda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/codenameredpanda/32/12565_2.png) [@codenameredpanda](https://community.auth0.com/u/codenameredpanda)\
**Post date:** [September 23, 2023, 10:11am UTC](https://community.auth0.com/t/nextjs13-auth0-redirect-user-based-on-certain-conditions-in-callback-method/116987/1 "2023-09-23T10:11:50Z")

</div>

Hi,

I am building a new application using NextJS13 + Auth0. NextJs version is `13.4.19` whereas auth0 lib version is `3.1.0`. I am using Server components. The file is `\app\api\auth\[auth0]\route.ts`

I have a custom callback handler

```auto
const afterCallback = async (req: NextRequest, session: Session, state: { [key: string]: any }) => {
    if (session.user.email_verified) {
        console.log('email verified..continuing')
        return session;
    } else {
      // redirect('http://localhost:3000/verify-email', RedirectType.replace);     
      // return NextResponse.redirect(new URL('http://localhost:3000/verify-email'))
       headers.set('location', '/verify-email');
    }

    console.log('printing after redirect')    
    return session;
};

```

My authHandlers

```auto
export const GET = handleAuth({
    callback: async (req: any, res: any) => {
        try {
            console.log('callback method called')
           return await handleCallback(req, res, {
                afterCallback,
               // redirectUri: 'http://localhost:3000/dashboard'
            });
        } catch (error) {
            console.error(error);
        }
    },

  login: async (req: NextApiRequest, res: NextApiResponse) => {
        console.log('login method called')
        return await handleLogin(req, res, {
            // authorizationParams: {
            // redirect_uri: 'http://localhost:3000/api/auth/callback',
            // },
            returnTo: `/dashboard`,
        });
    },

```

I am not sure how to redirect the user to the `/verify-email` page when the user’s email is not verified.

I tried different options but no luck. Using `redirect` throws an error as per docs.- [Functions: redirect | Next.js](https://nextjs.org/docs/app/api-reference/functions/redirect)  
There is another example in the docs using `headers.set('location', '/verify-email');`  
However that doesn’t work either as headers are read-only. I get the below error  
`CAUSE: next_headers __WEBPACK_IMPORTED_MODULE_1__.headers.set is not a function`

Does anyone know how I can push the user to `/verify-email` page in case the email is unverified. TIA

---

<div class="post-metadata">

**Author:** ![vinidroid](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vinidroid/32/51507_2.png) [@vinidroid](https://community.auth0.com/u/vinidroid)\
**Post date:** [October 9, 2023, 2:46am UTC](https://community.auth0.com/t/nextjs13-auth0-redirect-user-based-on-certain-conditions-in-callback-method/116987/2 "2023-10-09T02:46:21Z")

</div>

Hey @codenameredpanda I was going through the same thing.  
I got this working by using res.setHeader.

```auto
 if (!userDb) {
      res.setHeader('Location', '/401');
    }
  }

  return session

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![codenameredpanda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/codenameredpanda/32/12565_2.png) [@codenameredpanda](https://community.auth0.com/u/codenameredpanda)\
**Post date:** [October 13, 2023, 12:05am UTC](https://community.auth0.com/t/nextjs13-auth0-redirect-user-based-on-certain-conditions-in-callback-method/116987/3 "2023-10-13T00:05:03Z")

</div>

Hey @vinidroid , are you using `pages` or `app` directory in NextJs. While using `app` directory, the `afterCallback` method doesn’t have `response` as an argument ☹

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [December 7, 2024, 1:58am UTC](https://community.auth0.com/t/nextjs13-auth0-redirect-user-based-on-certain-conditions-in-callback-method/116987/4 "2024-12-07T01:58:45Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
