Skip to content

Commit

Permalink
Fix afterCallback docs for the app router (#1602)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjmcgrath committed Dec 7, 2023
2 parents afcc202 + 27f066f commit 9496916
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/handlers/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
* },
* });
* ```
*
Expand All @@ -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;
* },
* });
* ```
*
Expand Down

0 comments on commit 9496916

Please sign in to comment.