Skip to content

feat: add support for mounting auth routes as api routes#2489

Draft
frederikprijck wants to merge 1 commit intofeat/middlewareless-phase1from
feat/middlewareless-phase2
Draft

feat: add support for mounting auth routes as api routes#2489
frederikprijck wants to merge 1 commit intofeat/middlewareless-phase1from
feat/middlewareless-phase2

Conversation

@frederikprijck
Copy link
Member

@frederikprijck frederikprijck commented Jan 13, 2026

📋 Changes

This PR adds support for mounting the authentication routes with both Next.js' App Router and Pages Router, enabling developers to use our SDK without handling these in the middleware.

Usage

This PR introduces a new handleAuth method that works seamlessly with both routing patterns:

App Router usage:

// app/api/auth/[...auth0]/route.ts
import { auth0 } from "@/lib/auth0";

export const GET = auth0.handleAuth;
export const POST = auth0.handleAuth;

Pages Router usage:

// pages/api/auth/[...auth0].ts
import { auth0 } from "@/lib/auth0";

export default auth0.handleAuth;

Technical Changes

  • Enhanced AuthClient.handler (internal) method with method overloading to support both NextRequest (App Router/Middleware) and NextApiRequest (Pages Router)
  • Added new HTTP adapters (Auth0NextApiRequest, Auth0NextApiResponse, Auth0ApiResponseCookies) to handle Pages Router request/response objects
  • Extended OnCallbackHook context to include request and response objects for Pages Router compatibility
  • Added runtime detection using isRequest() to automatically determine the routing pattern and use appropriate handlers

📎 References

N/A

🎯 Testing

@frederikprijck frederikprijck changed the title feat: add support for app and pages router api routes and add handleAuth handler feat: add support for mounting auth routes as api routes Jan 13, 2026
@frederikprijck frederikprijck force-pushed the feat/middlewareless-phase2 branch from b437677 to 02bc237 Compare January 15, 2026 10:28
Comment on lines +128 to +141
/**
* The NextApiResponse object when using Pages Router.
*
* @remark This property is undefined when using the App Router, as the App Router uses NextResponse,
* which we do not have initially and should be created and returned instead.
*/
response?: NextApiResponse;

/**
* The request object (NextRequest for App Router or Middleware on Edge, NextApiRequest for Pages Router, or Request for Middleware or Node.js or Proxy).
*
* @remark This property is undefined when using the App Router.
*/
request?: NextRequest | NextApiRequest | Request;
Copy link
Member Author

@frederikprijck frederikprijck Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In App Router, users can return a NextResponse instance from onCallback. However, in Pages Router, the user should mutate the original NextApiResponse, so we need to expose response when using Pages Router.

Exposing request made things a bit simpler for the defaultOnCallback we have, and has been requested here, so feels like a useful thing to consider.

/**
* Mounts the SDK proxy routes to run as a proxy function.
*/
async proxy(req: NextRequest) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As our SDK has a capability to expose certain proxy endpoints, we keep these as part of the proxy integration.

When using our middleware, it would mount both the authentication routes and the proxy routes automatically. With this PR, authentication routes can be mounted through the API routes with either Pages or App Router, which does not include the proxy functionality. That needs to be configured in the customers proxy.ts file, by calling handleProxy().

@frederikprijck frederikprijck force-pushed the feat/middlewareless-phase2 branch from 02bc237 to 3b8dcf9 Compare January 16, 2026 12:44
@frederikprijck frederikprijck force-pushed the feat/middlewareless-phase2 branch from 3b8dcf9 to f9f1516 Compare January 16, 2026 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant