Skip to content
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

fix: retain original request in middleware handler #1919

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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});
});
});

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 @@ -245,7 +245,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