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

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

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

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

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

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

  return session

Hope this helps.

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 :frowning: