-
Notifications
You must be signed in to change notification settings - Fork 450
chore: introduce generic request to be able to handle different kind of requests #2484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
231133e to
c75cc47
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2484 +/- ##
==========================================
- Coverage 91.18% 91.09% -0.09%
==========================================
Files 39 46 +7
Lines 4694 4931 +237
Branches 980 1027 +47
==========================================
+ Hits 4280 4492 +212
- Misses 408 433 +25
Partials 6 6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
c75cc47 to
f34e2c9
Compare
| method: "GET" | ||
| }); | ||
| authClient.handleLogin = vi.fn(); | ||
| authClient.handleLogin = vi.fn().mockResolvedValue({}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original mock forhandleX() is actually not correctly configured. Every handleX() method is configured not to return undefined, while vi.fn() on the left does return undefined.
This worked fine for this test, as we never expected any response and handler() just returned whatever handleX() returns.
However, handleX() now returns an Auth0Response, which handler() now unwraps using #unwrapHandler(). Because of that, handler() expects handleX to return something.
In this case, we return {}, which would mean the unwrapped NextResponse will be undefined, just like the original test.
| const auth0Req = new Auth0NextRequest(request); | ||
|
|
||
| const auth0Res = new Auth0NextResponse(NextResponse.next()); | ||
|
|
||
| await authClient.handleLogin(auth0Req, auth0Res); | ||
|
|
||
| const response = auth0Res.res; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original tests call handleX(). In my opinion, handleX is an implementation detail. It's the handler called by handler() for the corresponding route (e.g. /auth/login for handleLogin()). In my opinion, this should have been a private method, and call handler() instead in the tests.
If this test would have called const response = await authClient.handler(request); instead of const response = await authClient.handleLogin(request);, this would have not required any change at all.
Personally, I think we should change this test to go through the handler(), and treat handleX as an implementation detail.
Happy to update the PR, or do a follow up PR. Additionally, also happy to leave the tests as is, as they are public methods, so they aren't configured as the real implementation details I consider them.
| const auth0Req = new Auth0NextRequest(req); | ||
| const auth0Res = new Auth0NextResponse(new NextResponse()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As handler() is our main public API, I ensured this does not require any change. Instead, the first thing it does now is create the Auth0NextRequest instance, holding the original NextRequest instance.
Additionally, it also prepares an Auth0NextResponse, which contains a placeholder response.
This change for Auth0NextResponse is a bit biased by the fact that I know we want to introduce support for NextApiResponse in a next PR. NextResponse and NextApiResponse are very different in usage:
NextResponse: Next.js expects you to create and return the instance.NextApiResponse: Next.js gives you the NextApiResponse for the NextApiRequest, and expects you tomutateit.
Because of that knowledge, the Auth0Request and Auth0Response classes already account for this.
Admittedly, this bias comes from the fact that this PR is created by pulling out the Auth0Request, Auth0Response and Middleware / AppRouter specific logic from a single branch that contains the full integration with PagesRouter and AppRouter without using middleware.
| const auth0Req = new Auth0NextRequest(req); | ||
| const auth0Res = new Auth0NextResponse(new NextResponse()); | ||
|
|
||
| let { pathname } = auth0Req.getUrl(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer use nextUrl, instead we go through the Auth0Request.getUrl method to handle the abstraction. Internally, for NextRequest, it will return nextUrl.
|
|
||
| const sanitizedPathname = removeTrailingSlash(pathname); | ||
| const method = req.method; | ||
| const method = auth0Req.getMethod(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer use .method, instead we go through the Auth0Request.getMethod() method to handle the abstraction. Internally, for NextRequest, it will return method.
|
|
||
| if (method === "GET" && sanitizedPathname === this.routes.login) { | ||
| return this.handleLogin(req); | ||
| return this.#unwrapHandler(() => this.handleLogin(auth0Req, auth0Res)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handleX() no longer returns a NextResponse. Instead, it returns an Auth0NextResponse. Instead of returning whatever handleX() returns, we have to unwrap the res property, containing the NextResponse instance, and return that one.
Because of the reason mentioned here, every handleX() method now also accepts an Auth0Response instance to do response manipulations directly on the provided Auth0Response instance.
| const res = NextResponse.next(); | ||
| const session = await this.sessionStore.get(req.cookies); | ||
|
|
||
| const session = await this.sessionStore.get(auth0Req.getCookies()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as nextUrl, and method, we should not try and read the cookies directly from NextRequest. Instead, we read them through Auth0Request, handling the NextRequest specific cookie handling.
| await this.sessionStore.set( | ||
| auth0Req.getCookies(), | ||
| auth0Res.getCookies(), | ||
| { | ||
| ...session | ||
| } | ||
| ); | ||
| auth0Res.addCacheControlHeadersForSession(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As manipulating headers differs between NextResponse and NextApiResponse, we should call this from Auth0Response instead.
| const session = await this.sessionStore.get(req.cookies); | ||
|
|
||
| const session = await this.sessionStore.get(auth0Req.getCookies()); | ||
| const auth0Res = new Auth0NextResponse(NextResponse.next()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, we just want to continue the next middleware, so we need to call NextResponse.next(), but wrapped in a new Auth0NextResponse.
Important: this is still bound to NextResponse. But this is fine as this is middleware only. We will need solve this in the. follow up PR to add support for using the SDK without middleware.
| } | ||
|
|
||
| return res; | ||
| return auth0Res.res; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to return the underlyingNextResponse.
src/server/auth-client.ts
Outdated
| async #unwrapHandler(handler: () => Promise<Auth0Response>): Promise<NextResponse> { | ||
| const auth0Response = await handler(); | ||
| return auth0Response.res; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, this only returns Promise<NextResponse>. This is fine for now, but in the follow-up PR that adds support for mounting the auth routes without middelware, we will change unwrapHandler to unwrap based on the context it's used in.
| } | ||
|
|
||
| async startInteractiveLogin( | ||
| auth0Res: Auth0Response, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the internal method, not exposed to our users. This is used by server/client.ts, the instance exposed to the user.
| if (this.logoutStrategy === "v2") { | ||
| // Always use v2 logout endpoint | ||
| logoutResponse = createV2LogoutResponse(); | ||
| createV2LogoutResponse(auth0Res); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of returning a new instance, we have to call methods on the Auth0Response.
| const errorRes = await this.onCallback(new InvalidStateError(), {}, null); | ||
| auth0Res.setResponse(errorRes); | ||
| return auth0Res; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit of a weird thing, but onCallback is a property that gets set to the user-provided options.onCallback, or used the defaultOnCallback. It's configured to return a NextResponse.
To make this as generic as possible, the Auth0Response contains a setResponse() that allows you to just set the underlying response.
| ); | ||
|
|
||
| await this.transactionStore.delete(res.cookies, state); | ||
| auth0Res.setResponse(res); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
|
||
| await this.sessionStore.set(req.cookies, res.cookies, session, true); | ||
| addCacheControlHeadersForSession(res); | ||
| auth0Res.setResponse(res); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| reqCookies = mocks.reqCookies; | ||
| resCookies = mocks.resCookies; | ||
| auth0ReqCookies = mocks.auth0ReqCookies; | ||
| auth0ResCookies = mocks.auth0ResCookies; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need both the original cookies, and the Auth0Cookies, because we need to pass Auth0Cookies instances down to setChunkedCookie, but we want to still expect the set method to have been called on the original cookie instance.
| ): Promise<NextResponse> { | ||
| return this.authClient.startInteractiveLogin(options); | ||
| const auth0Res = await this.authClient.startInteractiveLogin( | ||
| new Auth0NextResponse(new NextResponse()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
startInteractiveLogin needs to set a cookie on the response. Before this PR, it would internally create and return NextResponse instances. However, we are changing this with this PR by providing the Auth0NextResponse object, wrapping a placeholder NextResponse, and have the startInteractive call corresponding method on the Auth0NextResponse instance (e.g. redirect(), as well as getCookies() to retrieve the cookies for the response, allowing the SDK to add a cookie as needed.
This is mostly changed with the knowledge that for Pages Router, Next.js will provide the NextApiResponse initially together with the NextApiRequest. But for the App Router, we only receive a NextRequest, and we should instantiate and return NextResponse ourselves. To unify this, we now always expect an Auth0Response implementation.
📋 Changes
Introduces an abstraction layer to decouple authentication logic from Next.js-specific
NextRequest/NextResponsetypes. This refactor enables future support for API routes with Pages Router without duplicating authentication code.Impact: No breaking changes to external APIs. This is purely an internal refactor - middleware continues to work exactly as before.
Motivation
The current v4 implementation is tightly coupled to Next.js middleware types (
NextRequest/NextResponse). Supporting API routes or Pages Router would require duplicating all authentication logic forNextApiRequest/NextApiResponse, or adding someif/elsebrancing.This abstraction establishes a foundation for multi-paradigm support through polymorphism rather than code duplication.
Overview
New HTTP layer (
src/server/http/):Auth0Request<TRequest>- Abstract base class for request operationsAuth0Response<TResponse>- Abstract base class for response operationsAuth0NextRequest/Auth0NextResponse- Concrete implementations for middlewareAuth0RequestCookies/Auth0ResponseCookies- Unified cookie interfacesKey design decisions:
Auth0Request<NextRequest>maintains full type information through the stackRequestCookiesandReadonlyRequestCookiesCritical Changes & Patterns
1. Handler Abstraction Pattern
All authentication handlers now accept abstracted types instead of concrete Next.js types:
This enables the same handler logic to work with any request/response implementation (middleware, API routes, route handlers).
2. Response Wrapper & Header Merging
Introduced
#unwrapHandler()helper inauth-client.tsthat:NextResponsefrom `Auth0Response.resNextResponseinstance.The response wrapper pattern allows handlers to remain agnostic of the concrete response type while maintaining compatibility with Next.js middleware expectations in a non-breaking way.
3. Session & Transaction Store Refactoring
All storage abstractions now operate on
Auth0RequestCookies/Auth0ResponseCookies:AbstractSessionStore- Updated interface for get/set/delete operationsStatelessSessionStore- Cookie encoding/chunking logic works with abstractionsStatefulSessionStore- Database operations use abstracted cookie interfacesTransactionStore- State parameter handling via abstractionsThis enables future storage implementations to work seamlessly across different Next.js contexts.
4. Response
When using NextResponse, we typically need to create a new instance and return it. However, to support Pages Router, which uses NextApiResponse, we know that it will be different in the sense that we will accept a NextApiResponse and call methods on that instance rather than return new instances. Therefore, this PR already ensures we create an Auth0Response in the beginning of all handlers, and returns the underlying
Auth0Response.res.This may be a bit weird looking at it purely from the App Router perspective, but is done to support Pages Router in a follow up PR.
📎 References
N/A
🎯 Testing