Skip to content

Commit f9cea0e

Browse files
fix: retain original request in middleware handler
we noticed that custom headers from the original request were not retained as `NextResponse.next` was called without the original request
1 parent 724310c commit f9cea0e

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/server/auth-client.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ ca/T0LLtgmbMmxSv/MmzIg==
464464
});
465465

466466
it("should pass the request through if there is no session", async () => {
467+
const spyOnNextResponseNext = vi.spyOn(NextResponse, 'next');
468+
467469
const secret = await generateSecret(32);
468470
const transactionStore = new TransactionStore({
469471
secret
@@ -489,10 +491,14 @@ ca/T0LLtgmbMmxSv/MmzIg==
489491
fetch: getMockAuthorizationServer()
490492
});
491493

494+
const headers = new Headers();
495+
headers.append("x-custom-header", `custom-header-value`);
496+
492497
const request = new NextRequest(
493498
"https://example.com/dashboard/projects",
494499
{
495-
method: "GET"
500+
method: "GET",
501+
headers
496502
}
497503
);
498504

@@ -504,6 +510,9 @@ ca/T0LLtgmbMmxSv/MmzIg==
504510
// assert session has not been updated
505511
const updatedSessionCookie = response.cookies.get("__session");
506512
expect(updatedSessionCookie).toBeUndefined();
513+
514+
// assert that an original request is retained
515+
expect(spyOnNextResponseNext).toHaveBeenCalledWith({request});
507516
});
508517
});
509518

src/server/auth-client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export class AuthClient {
245245
// no auth handler found, simply touch the sessions
246246
// TODO: this should only happen if rolling sessions are enabled. Also, we should
247247
// try to avoid reading from the DB (for stateful sessions) on every request if possible.
248-
const res = NextResponse.next();
248+
const res = NextResponse.next({request: req});
249249
const session = await this.sessionStore.get(req.cookies);
250250

251251
if (session) {

0 commit comments

Comments
 (0)