feat: add support for mounting auth routes as api routes#2489
feat: add support for mounting auth routes as api routes#2489frederikprijck wants to merge 1 commit intofeat/middlewareless-phase1from
Conversation
b437677 to
02bc237
Compare
| /** | ||
| * 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; |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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().
02bc237 to
3b8dcf9
Compare
3b8dcf9 to
f9f1516
Compare
📋 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
handleAuthmethod that works seamlessly with both routing patterns:App Router usage:
Pages Router usage:
Technical Changes
AuthClient.handler(internal) method with method overloading to support bothNextRequest(App Router/Middleware) andNextApiRequest(Pages Router)Auth0NextApiRequest,Auth0NextApiResponse,Auth0ApiResponseCookies) to handle Pages Router request/response objectsOnCallbackHookcontext to includerequestandresponseobjects for Pages Router compatibilityisRequest()to automatically determine the routing pattern and use appropriate handlers📎 References
N/A
🎯 Testing