Skip to content

handleCallback post-processing #1613

@jamesarosen

Description

@jamesarosen

Checklist

  • I have looked into the Readme, Examples, and FAQ and have not found a suitable solution or answer.
  • I have looked into the API documentation and have not found a suitable solution or answer.
  • I have searched the issues and have not found a suitable solution or answer.
  • I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • I agree to the terms within the Auth0 Code of Conduct.

Describe the problem you'd like to have solved

I'm trying to do some post-processing in the callback handler:

// pages/api/auth/[auth0].ts

export default handleAuth({
  callback: async (req: NextApiRequest, res: NextApiResponse) => {
    // default session initialization:
    await handleCallback(req, res);

    // store the token in the backend for authentication / authorization:
    const { accessToken } = await getAccessToken(req, res);
    const success = await db.addValidAccessToken(accessToken);
    if (!success) {
      // what to do here?
    }
  }
})

Option 1: Throw an Error

if (!success) {
  const error = new Error('Error signing in');
  error.status = error.statusCode = 500;
  throw error;
}

The problem with this is that @auth0/nextjs-auth0 overrides my status with 400 and its own error message. The UI is quite poor.

Option 2: Modify res

if (!success) {
  res.redirect('/login-error');
}

This doesn't do anything because the original handler succeeded, so the modifications to the response are discarded.

Describe the ideal solution

I'd like to be able to replace the Response with one of my own even if the callback succeeded. Ideally, I'd like to be able to render my application's error page.

Alternatives and current workarounds

No response

Additional context

The handleCallback docs have some information on customizing the callback, but not on modifying the response or much on error handling.

This issue suggests using res.redirect, but that doesn't work here because handleCallback already finalized res.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions