Skip to content

Commit

Permalink
fix: retain original request in middleware handler
Browse files Browse the repository at this point in the history
we noticed that custom headers from the original request were not retained as `NextResponse.next` was called without the original request
  • Loading branch information
IdanLupinskyMyPorsche committed Feb 14, 2025
1 parent caa7f45 commit 262d9d6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/server/auth-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ ca/T0LLtgmbMmxSv/MmzIg==
});

it("should pass the request through if there is no session", async () => {
const spyOnNextResponseNext = vi.spyOn(NextResponse, 'next');

const secret = await generateSecret(32);
const transactionStore = new TransactionStore({
secret
Expand All @@ -489,10 +491,14 @@ ca/T0LLtgmbMmxSv/MmzIg==
fetch: getMockAuthorizationServer()
});

const headers = new Headers();
headers.append("x-custom-header", `custom-header-value`);

const request = new NextRequest(
"https://example.com/dashboard/projects",
{
method: "GET"
method: "GET",
headers
}
);

Expand All @@ -504,6 +510,9 @@ ca/T0LLtgmbMmxSv/MmzIg==
// assert session has not been updated
const updatedSessionCookie = response.cookies.get("__session");
expect(updatedSessionCookie).toBeUndefined();

// assert that an original request is retained
expect(spyOnNextResponseNext).toHaveBeenCalledWith({request})

Check notice

Code scanning / CodeQL

Semicolon insertion Note

Avoid automated semicolon insertion (92% of all statements in
the enclosing function
have an explicit semicolon).
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/server/auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export class AuthClient {
// no auth handler found, simply touch the sessions
// TODO: this should only happen if rolling sessions are enabled. Also, we should
// try to avoid reading from the DB (for stateful sessions) on every request if possible.
const res = NextResponse.next();
const res = NextResponse.next({request: req});
const session = await this.sessionStore.get(req.cookies);

if (session) {
Expand Down

0 comments on commit 262d9d6

Please sign in to comment.