Skip to content
This repository was archived by the owner on Apr 26, 2025. It is now read-only.

Commit 73b4629

Browse files
committed
Next.js: ignore prefetch request for /logout middleware route
1 parent 7e17298 commit 73b4629

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/nextjs/index.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe('middleware', () => {
122122
},
123123
]);
124124

125-
describe('logout', () => {
125+
describe('login', () => {
126126
it('should redirect to Fief authentication URL', async () => {
127127
const request = new NextRequest('http://localhost:3000/login');
128128
const response = await middleware(request);
@@ -156,6 +156,15 @@ describe('middleware', () => {
156156
});
157157

158158
describe('logout', () => {
159+
it('should do nothing and just return empty response on prefetch', async () => {
160+
const request = new NextRequest('http://localhost:3000/logout', { headers: { 'X-Middleware-Prefetch': '1' } });
161+
const response = await middleware(request);
162+
163+
expect(response.status).toBe(204);
164+
165+
expect(response.cookies.get('user_session')).toBeUndefined();
166+
});
167+
159168
it('should clear session cookie and redirect to Fief logout URL', async () => {
160169
const request = new NextRequest('http://localhost:3000/logout');
161170
const response = await middleware(request);

src/nextjs/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,13 @@ class FiefAuth {
314314
authenticate: this.fiefAuthEdge.authenticate(parameters),
315315
}));
316316
return async (request: NextRequest): Promise<NextResponse> => {
317+
const isPrefetchRequest = request.headers.get('X-Middleware-Prefetch') === '1';
318+
317319
// Handle login
318320
if (request.nextUrl.pathname === this.loginPath) {
321+
if (isPrefetchRequest) {
322+
return new NextResponse(null, { status: 204 });
323+
}
319324
const authURL = await this.client.getAuthURL({ redirectURI: this.redirectURI, scope: ['openid'] });
320325
const response = NextResponse.redirect(authURL);
321326
const returnTo = request.nextUrl.searchParams.get('return_to');
@@ -327,6 +332,9 @@ class FiefAuth {
327332

328333
// Handle authentication callback
329334
if (request.nextUrl.pathname === this.redirectPath) {
335+
if (isPrefetchRequest) {
336+
return new NextResponse(null, { status: 204 });
337+
}
330338
const code = request.nextUrl.searchParams.get('code');
331339
const [tokens] = await this.client.authCallback(code as string, this.redirectURI);
332340

@@ -349,6 +357,9 @@ class FiefAuth {
349357

350358
// Handle logout
351359
if (request.nextUrl.pathname === this.logoutPath) {
360+
if (isPrefetchRequest) {
361+
return new NextResponse(null, { status: 204 });
362+
}
352363
const logoutURL = await this.client.getLogoutURL({ redirectURI: this.logoutRedirectURI });
353364
const response = NextResponse.redirect(logoutURL);
354365
response.cookies.set(this.sessionCookieName, '', { maxAge: 0 });

0 commit comments

Comments
 (0)