nextjs-auth0 icon indicating copy to clipboard operation
nextjs-auth0 copied to clipboard

How do I properly deal with CallbackHandlerError?

Open DarthHater opened this issue 8 months ago • 0 comments

Checklist

  • [X] The issue can be reproduced in the nextjs-auth0 sample app (or N/A).
  • [X] I have looked into the Readme, Examples, and FAQ and have not found a suitable solution or answer.
  • [X] I have looked into the API documentation and have not found a suitable solution or answer.
  • [X] I have searched the issues and have not found a suitable solution or answer.
  • [X] I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • [X] I agree to the terms within the Auth0 Code of Conduct.

Description

I am working on an app where we are attempting to deal with unverified emails by denying access via Actions in auth0.

I am using NextJS 14, and the newest version of this library.

I continually get this error:

CallbackHandlerError: Callback handler failed. CAUSE: access_denied (error_email_unverified)

It ALMOST looks like log spam, because I can write code that deals with the actual error (and it appears to work). However, I have tried to write a number of different things to clean up this potential log spam, and to no avail.

I have:

    onError: (
      req: NextApiRequest,
      res: NextApiResponse,
      error: HandlerError,
    ) => {
      console.error(error);
      res.writeHead(302, { Location: '/error' });
      res.end();
    },

I am trying that just to see if I can write something that logs when an error occurs (it does not).

Our callback caller looks like callback: getCallbackHandler(auth0Server),, and this is to setup something akin to:

return auth0Server.handleAuth({
    login: getHandleLogin(auth0Server),
    logout: getHandleLogout(auth0Server),
    callback: getCallbackHandler(auth0Server),
    onError: (
      req: NextApiRequest,
      res: NextApiResponse,
      error: HandlerError,
    ) => {
      console.error(error);
      res.writeHead(302, { Location: '/error' });
      res.end();
    },
  });

This calls:

const getCallbackHandler = (auth0Server: Auth0Server) => {
  return async (req: NextApiRequest, res: NextApiResponse) => {
    try {
      if (
        req.query &&
        req.query.error &&
        req.query.error === 'access_denied' &&
        req.query.error_description &&
        req.query.error_description === 'error_email_unverified'
      ) {
        const state: Auth0State = JSON.parse(
          Buffer.from(req.query.state as string, 'base64').toString(),
        );

        if (
         CONDITION
        ) {
          res.redirect(getRootURL(state.returnTo));
        } else {
          res.redirect(state.returnTo);
        }

        res.end();
      }

      await auth0Server.handleCallback(req, res, {
        afterCallback: handleAuth0Callback,
        redirectUri: req.headers.referer,
      });
    } catch (err) {
      console.error(err);
    }
  };
};

Your documentation is a little hazy at best for dealing with these things, it's not exactly clear what onError does, it kind of appears to do nothing in practice.

I've tried try catch blocks around stuff, no avail.

This log spam is causing distractions in production and it is difficult to ascertain if there is a real issue as a result.

How do I catch this properly?

Fuller log statement with some info removed:

CallbackHandlerError: Callback handler failed. CAUSE: access_denied (error_email_unverified)
    at /application_i_am_working_on/node_modules/@auth0/nextjs-auth0/dist/handlers/callback.js:78:15
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async eval (webpack-internal:///(api)/./pages/api/auth/[...auth0].ts:94:13)
    ... 9 lines matching cause stack trace ...
    at async invokeRender (/application_i_am_working_on/node_modules/next/dist/server/lib/router-server.js:174:21)
    at async handleRequest (/application_i_am_working_on/node_modules/next/dist/server/lib/router-server.js:353:24)
    at async requestHandlerImpl (/application_i_am_working_on/node_modules/next/dist/server/lib/router-server.js:377:13) {
  code: 'ERR_CALLBACK_HANDLER_FAILURE',
  cause: IdentityProviderError: access_denied (error_email_unverified)
      at NodeClient.callback (/application_i_am_working_on/node_modules/@auth0/nextjs-auth0/dist/auth0-session/client/node-client.js:132:23)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async /application_i_am_working_on/node_modules/@auth0/nextjs-auth0/dist/auth0-session/handlers/callback.js:43:29
      at async /application_i_am_working_on/node_modules/@auth0/nextjs-auth0/dist/handlers/callback.js:75:16
      at async eval (webpack-internal:///(api)/./pages/api/auth/[...auth0].ts:94:13)
      at async /application_i_am_working_on/node_modules/@auth0/nextjs-auth0/dist/handlers/auth.js:79:13
      at async K (/application_i_am_working_on/node_modules/next/dist/compiled/next-server/pages-api.runtime.dev.js:21:2871)
      at async U.render (/application_i_am_working_on/node_modules/next/dist/compiled/next-server/pages-api.runtime.dev.js:21:3955)
      at async DevServer.runApi (/application_i_am_working_on/node_modules/next/dist/server/next-server.js:600:9)
      at async NextNodeServer.handleCatchallRenderRequest (/application_i_am_working_on/node_modules/next/dist/server/next-server.js:269:37)
      at async DevServer.handleRequestImpl (/application_i_am_working_on/node_modules/next/dist/server/base-server.js:816:17)
      at async /application_i_am_working_on/node_modules/next/dist/server/dev/next-dev-server.js:339:20
      at async Span.traceAsyncFn (/application_i_am_working_on/node_modules/next/dist/trace/trace.js:154:20)
      at async DevServer.handleRequest (/application_i_am_working_on/node_modules/next/dist/server/dev/next-dev-server.js:336:24)
      at async invokeRender (/application_i_am_working_on/node_modules/next/dist/server/lib/router-server.js:174:21) {
    error: 'access_denied',
    errorDescription: 'error_email_unverified',
    status: 400,
    statusCode: 400,
    openIdState: { returnTo: 'http://localhost:3000/' }
  },
  status: 400
}

Reproduction

If absolutely necessary I will provide some reproduction steps, this is more about how to actually use the API correctly

Additional context

No response

nextjs-auth0 version

3.5.0

Next.js version

14.2.3

Node.js version

18.19.0

DarthHater avatar May 27 '24 20:05 DarthHater