From dd70d358ab254a0b3103ae99410eb4b5a709b73a Mon Sep 17 00:00:00 2001 From: Adam Mcgrath Date: Thu, 7 Dec 2023 14:16:11 +0000 Subject: [PATCH] Fix afterCallback docs for the app router --- src/handlers/callback.ts | 43 ++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/handlers/callback.ts b/src/handlers/callback.ts index f558eaa2..1d9567fe 100644 --- a/src/handlers/callback.ts +++ b/src/handlers/callback.ts @@ -114,18 +114,23 @@ export type AfterCallbackPageRoute = ( * ```js * // app/api/auth/[auth0]/route.js * import { handleAuth, handleCallback } from '@auth0/nextjs-auth0'; - * import { redirect } from 'next/navigation'; + * import { NextResponse } from 'next/server'; * - * const afterCallback = (req, session, state) => { + * const afterCallback = (req, session) => { * if (session.user.isAdmin) { * return session; - * } else { - * redirect('/unauthorized'); * } * }; - * - * export default handleAuth({ - * callback: handleCallback({ afterCallback }) + + * export const GET = handleAuth({ + * async callback(req, ctx) { + * const res = await handleCallback(req, ctx, { afterCallback }); + * const session = await getSession(req, res); + * if (!session) { + * return NextResponse.redirect(`${process.env.AUTH0_BASE_URL}/fail`, res); + * } + * return res; + * }, * }); * ``` * @@ -142,27 +147,27 @@ export type AfterCallbackPageRoute = ( * return session; * }; * - * export default handleAuth({ + * export const GET = handleAuth({ * callback: handleCallback({ afterCallback }) * }); * ``` * - * @example Redirect successful login based on claim + * @example Redirect successful login based on claim (afterCallback is not required). * * ```js * // pages/api/auth/[auth0].js * import { handleAuth, handleCallback } from '@auth0/nextjs-auth0'; - * import { headers } from 'next/headers'; - * - * const afterCallback = (req, session, state) => { - * if (!session.user.isAdmin) { - * headers.set('location', '/admin'); - * } - * return session; - * }; + * import { NextResponse } from 'next/server'; * - * export default handleAuth({ - * callback: handleCallback({ afterCallback }) + * export const GET = handleAuth({ + * async callback(req, ctx) { + * const res = await handleCallback(req, ctx); + * const session = await getSession(req, res); + * if (session?.user.isAdmin) { + * return NextResponse.redirect(`${process.env.AUTH0_BASE_URL}/admin`, res); + * } + * return res; + * }, * }); * ``` *