-
Notifications
You must be signed in to change notification settings - Fork 282
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(nextjs): Skip NextResponse normalization in mergeResponses if possible #2244
Merged
LekoArts
merged 6 commits into
main
from
lekoarts/sdk-996-authmiddleware-corrupts-response-cookies-only-emits-first
Dec 5, 2023
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
29eab33
fix(nextjs): If res is NextResponse just return it
LekoArts 7ee4e4b
fix(nextjs): Support options when setting cookies
LekoArts b63d276
chore(repo): Add initial next middleware test
LekoArts 8f05c68
chore(repo): Make test work
LekoArts b34f243
chore(repo): Add changeset
LekoArts 18ae827
Update packages/nextjs/src/utils/response.test.ts
LekoArts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@clerk/nextjs': patch | ||
--- | ||
|
||
Ensure that cookies set inside Next.js Middleware are correctly passed through while using [`authMiddleware`](https://clerk.com/docs/references/nextjs/auth-middleware). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { expect, test } from '@playwright/test'; | ||
|
||
import type { Application } from '../models/application'; | ||
import { appConfigs } from '../presets'; | ||
import { createTestUtils } from '../testUtils'; | ||
|
||
test.describe('next middleware @nextjs', () => { | ||
test.describe.configure({ mode: 'parallel' }); | ||
let app: Application; | ||
|
||
test.beforeAll(async () => { | ||
app = await appConfigs.next.appRouter | ||
.clone() | ||
.addFile( | ||
'src/middleware.ts', | ||
() => `import { authMiddleware } from '@clerk/nextjs/server'; | ||
import { NextResponse } from "next/server"; | ||
|
||
export default authMiddleware({ | ||
publicRoutes: ['/', '/hash/sign-in', '/hash/sign-up'], | ||
afterAuth: async (auth, req) => { | ||
const response = NextResponse.next(); | ||
response.cookies.set({ | ||
name: "first", | ||
value: "123456789", | ||
path: "/", | ||
sameSite: "none", | ||
secure: true, | ||
}); | ||
response.cookies.set("second", "987654321", { | ||
sameSite: "none", | ||
secure: true, | ||
}); | ||
response.cookies.set("third", "foobar", { | ||
sameSite: "none", | ||
secure: true, | ||
}); | ||
return response; | ||
}, | ||
}); | ||
|
||
export const config = { | ||
matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'], | ||
};`, | ||
) | ||
.addFile( | ||
'src/app/provider.tsx', | ||
() => `'use client' | ||
import { ClerkProvider } from "@clerk/nextjs" | ||
|
||
export function Provider({ children }: { children: any }) { | ||
return ( | ||
<ClerkProvider> | ||
{children} | ||
</ClerkProvider> | ||
) | ||
}`, | ||
) | ||
.addFile( | ||
'src/app/layout.tsx', | ||
() => `import './globals.css'; | ||
import { Inter } from 'next/font/google'; | ||
import { Provider } from './provider'; | ||
|
||
const inter = Inter({ subsets: ['latin'] }); | ||
|
||
export const metadata = { | ||
title: 'Create Next App', | ||
description: 'Generated by create next app', | ||
}; | ||
|
||
export default function RootLayout({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<Provider> | ||
<html lang='en'> | ||
<body className={inter.className}>{children}</body> | ||
</html> | ||
</Provider> | ||
); | ||
} | ||
`, | ||
) | ||
.commit(); | ||
await app.setup(); | ||
await app.withEnv(appConfigs.envs.withEmailCodes); | ||
await app.dev(); | ||
}); | ||
|
||
test.afterAll(async () => { | ||
await app.teardown(); | ||
}); | ||
|
||
test('authMiddleware passes through all cookies', async ({ browser }) => { | ||
// See https://playwright.dev/docs/api/class-browsercontext | ||
const context = await browser.newContext(); | ||
const page = await context.newPage(); | ||
const u = createTestUtils({ app, page }); | ||
|
||
await page.goto(app.serverUrl); | ||
await u.po.signIn.waitForMounted(); | ||
|
||
const cookies = await context.cookies(); | ||
|
||
expect(cookies.find(c => c.name == 'first').value).toBe('123456789'); | ||
expect(cookies.find(c => c.name == 'second').value).toBe('987654321'); | ||
expect(cookies.find(c => c.name == 'third').value).toBe('foobar'); | ||
|
||
await context.close(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,10 +29,33 @@ describe('mergeResponses', function () { | |
const response1 = new NextResponse(); | ||
const response2 = new NextResponse(); | ||
response1.cookies.set('foo', '1'); | ||
response1.cookies.set('second', '2'); | ||
response1.cookies.set('bar', '1'); | ||
response2.cookies.set('bar', '2'); | ||
const finalResponse = mergeResponses(response1, response2); | ||
expect(finalResponse!.cookies.get('foo')).toEqual(response1.cookies.get('foo')); | ||
expect(finalResponse!.cookies.get('second')).toEqual(response1.cookies.get('second')); | ||
expect(finalResponse!.cookies.get('bar')).toEqual(response2.cookies.get('bar')); | ||
}); | ||
|
||
it('should merge the cookies with non-response values', function () { | ||
const response2 = NextResponse.next(); | ||
console.log(response2); | ||
LekoArts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
response2.cookies.set('foo', '1'); | ||
response2.cookies.set({ | ||
name: 'second', | ||
value: '2', | ||
path: '/', | ||
sameSite: 'none', | ||
secure: true, | ||
}); | ||
response2.cookies.set('bar', '1', { | ||
sameSite: 'none', | ||
secure: true, | ||
}); | ||
Comment on lines
+43
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The user reproduction used this format and while adding it the tests failed because |
||
const finalResponse = mergeResponses(null, response2); | ||
expect(finalResponse!.cookies.get('foo')).toEqual(response2.cookies.get('foo')); | ||
expect(finalResponse!.cookies.get('second')).toEqual(response2.cookies.get('second')); | ||
expect(finalResponse!.cookies.get('bar')).toEqual(response2.cookies.get('bar')); | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 test did not run without these additions. The
next-build.test.ts
file has the same additions.Why is the base template not just including these?