AfterCallbackAppRoute: ((req, session, state?) => Promise<default | Response | undefined> | default | Response | undefined)

Type declaration

    • (req, session, state?): Promise<default | Response | undefined> | default | Response | undefined
    • Use this function for validating additional claims on the user's ID token or adding removing items from the session after login.

      Parameters

      • req: NextRequest
      • session: default
      • Optional state: {
            [key: string]: any;
        }
        • [key: string]: any

      Returns Promise<default | Response | undefined> | default | Response | undefined

      Example

      Validate additional claims

      // app/api/auth/[auth0]/route.js
      import { handleAuth, handleCallback } from '@auth0/nextjs-auth0';
      import { redirect } from 'next/navigation';

      const afterCallback = (req, session, state) => {
      if (session.user.isAdmin) {
      return session;
      } else {
      redirect('/unauthorized');
      }
      };

      export default handleAuth({
      callback: handleCallback({ afterCallback })
      });

      Example

      Modify the session after login

      // pages/api/auth/[auth0].js
      import { handleAuth, handleCallback } from '@auth0/nextjs-auth0';
      import { NextResponse } from 'next/server';

      const afterCallback = (req, session, state) => {
      session.user.customProperty = 'foo';
      delete session.refreshToken;
      return session;
      };

      export default handleAuth({
      callback: handleCallback({ afterCallback })
      });

      Example

      Redirect successful login based on claim

      // 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;
      };

      export default handleAuth({
      callback: handleCallback({ afterCallback })
      });

      Throws

      HandlerError