AfterCallbackPageRoute: ((req, res, session, state?) => Promise<default | undefined> | default | undefined)

Type declaration

    • (req, res, session, state?): Promise<default | undefined> | default | 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: NextApiRequest
      • res: NextApiResponse
      • session: default
      • Optional state: {
            [key: string]: any;
        }
        • [key: string]: any

      Returns Promise<default | undefined> | default | undefined

      Example

      Validate additional claims

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

      const afterCallback = (req, res, session, state) => {
      if (session.user.isAdmin) {
      return session;
      } else {
      res.status(401).end('User is not admin');
      }
      };

      export default handleAuth({
      async callback(req, res) {
      try {
      await handleCallback(req, res, { afterCallback });
      } catch (error) {
      res.status(error.status || 500).end();
      }
      }
      });

      Example

      Modify the session after login

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

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

      export default handleAuth({
      async callback(req, res) {
      try {
      await handleCallback(req, res, { afterCallback });
      } catch (error) {
      res.status(error.status || 500).end();
      }
      }
      });

      Example

      Redirect successful login based on claim

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

      const afterCallback = (req, res, session, state) => {
      if (!session.user.isAdmin) {
      res.setHeader('Location', '/admin');
      }
      return session;
      };

      export default handleAuth({
      async callback(req, res) {
      try {
      await handleCallback(req, res, { afterCallback });
      } catch (error) {
      res.status(error.status || 500).end(error.message);
      }
      }
      });

      Throws

      HandlerError