I have tried researching for ways for this to work but couldn’t so here I am.
Context: When logging in, i want to redirect user accordingly based on their existence in the records.
Issue facing: With current code, the redirection gets neglected and user gets returned to the default page every single time.
Code-
import verifyUser from '@/hooks/auth/verifyUser';
import {
Session,
handleCallback,
handleAuth,
handleLogin,
} from '@auth0/nextjs-auth0';
import { NextRequest, NextResponse } from 'next/server';
const afterCallback = async (req: NextRequest, session: Session) => {
if (!session || session.accessToken === undefined) {
return NextResponse.redirect(new URL('/api/auth/logout', req.url));
}
const response = await verifyUser(session.accessToken, session.user.email);
const { exists } = response.data;
if (!exists) {
console.log('session-', session.user);
const redirectUrl = `${req.nextUrl.origin}/new-client`;
console.log('Redirecting to:', redirectUrl);
return {
returnTo: redirectUrl,
...session,
};
}
return NextResponse.json(session);
};
export const GET = handleAuth({
callback: handleCallback({ afterCallback }),
login: handleLogin({
authorizationParams: {
scope: 'openid profile email',
},
}),
});
Additional context-
- Currently using app based router in nextjs v14+ with latest auth0 package
- Can see the console log statement with expected redirectUrl value
Thankyou!