# NextJS/auth0 - custom callback handler not working as per documented examples

**URL:** <https://community.auth0.com/t/nextjs-auth0-custom-callback-handler-not-working-as-per-documented-examples/116661>\
**Category:** Get Help\
**Tags:** login-experience, new-universal-login-experience\
**Created:** [September 19, 2023, 11:29pm UTC](https://community.auth0.com/t/nextjs-auth0-custom-callback-handler-not-working-as-per-documented-examples/116661 "2023-09-19T23:29:41Z")\
**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 19, 2023, 11:29pm UTC](https://community.auth0.com/t/nextjs-auth0-custom-callback-handler-not-working-as-per-documented-examples/116661/1 "2023-09-19T23:29:41Z")

</div>

Hi,

I am trying to use `nextjs/auth0` library. NextJs versions is `13.4.19` whereas auth0 lib version is `3.1.0`.  
I have got the basic authentication working. Now, what I am trying to do is as follows:  
If the user’s email id is not verified I want to redirect the user to a different page - `/verify-email`. If the email id is verified, user should be redirected to `/dashboard`  
I thought of using `callbackHandler` for this. I followed the given example however it isn’t working as expected.

My code is as follows

```auto
export const GET = handleAuth({
    callback: async (req: NextApiRequest, res: NextApiResponse) => {
        try {
           return await handleCallback(req, res, {
                afterCallback
            });
        } catch (error) {
            console.error(error);
        }
    }
})

```

My callback method is

```auto
const afterCallback = async (req: NextApiRequest, res: NextApiResponse, session: Session) => {
    console.log('I am getting called after callback', session)
    if (session.user) {
        // permanentRedirect('/verify-email')
        // res.status(200).redirect('/verify-email')
        console.log('user found', session.user)
    } else {
        console.log('user not found', session)
    }
        
    return session;
};

```

However, I am not seeing any session details here. It just prints

```auto
I am getting called after callback { returnTo: 'http://localhost:3000' }
user not found { returnTo: 'http://localhost:3000' }

```

I am not sure what’s going wrong here. I was expecting to see the session details. I was planning to use `session.user.email_verified` to perform the check.

One additional thing I am confused about is - `returnTo` attribute of the `LoginOption` that’s passed to `handleLogin`. As per docs, its `URL to return to after login. Overrides the default in {@link BaseConfig.baseURL}`.  
Isn’t `callbackUrl` meant to be exactly that (Again as per docs - `A callback URL is a URL in your application where Auth0 redirects the user after they have authenticated`). I am confused between the two - when to use which and what should be their value respectively. If I don’t set `returnTo` I am returned to the home screen `http://localhost:3000`

Need some pointers here as I have been stuck on this for days now ☹ . TIA

---

<div class="post-metadata">

**Author:** ![TigerC10](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/tigerc10/32/52234_2.png) [@TigerC10](https://community.auth0.com/u/TigerC10)\
**Post date:** [November 6, 2023, 12:07am UTC](https://community.auth0.com/t/nextjs-auth0-custom-callback-handler-not-working-as-per-documented-examples/116661/2 "2023-11-06T00:07:21Z")

</div>

The function signature on your [afterCallback](https://auth0.github.io/nextjs-auth0/types/handlers_callback.AfterCallbackAppRoute.html) is incorrect. The afterCallback doesn’t receive the response. Second argument is the session, third argument is the login state.

```auto
const afterCallback = async (req: NextApiRequest, session: Session, state: GetLoginState) => {
    console.log('I am getting called after callback', session)
    if (session.user) {
        // permanentRedirect('/verify-email')
        // res.status(200).redirect('/verify-email')
        console.log('user found', session.user)
    } else {
        console.log('user not found', session)
    }
        
    return session;
};

```

---

<div class="post-metadata">

**Author:** ![alex.brett](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alex.brett/32/60157_2.png) [@alex.brett](https://community.auth0.com/u/alex.brett)\
**Post date:** [December 14, 2024, 12:04am UTC](https://community.auth0.com/t/nextjs-auth0-custom-callback-handler-not-working-as-per-documented-examples/116661/3 "2024-12-14T00:04:32Z")

</div>

Hello @codenameredpanda!

I understand you’re trying to redirect users to different pages depending on email verification. This is possible through a post-login action. You can use the `email_verified` property in the user raw json or add it to `app_metadata` where it may be easier to access in actions.

Once you have this information available in actions you can redirect the user to a verify email page through an action using the `post-login` trigger

I hope this helps!

Best,  
Alex

---

<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 28, 2024, 10:59am UTC](https://community.auth0.com/t/nextjs-auth0-custom-callback-handler-not-working-as-per-documented-examples/116661/5 "2024-12-28T10:59:00Z")

</div>

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