-
Notifications
You must be signed in to change notification settings - Fork 460
feat(nextjs): Read dynamic keys from clerkClient within middleware runtime
#3617
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
Merged
LauraBeatris
merged 9 commits into
propagate-nextjs-middleware-options
from
read-dynamic-keys-from-clerk-client
Jun 27, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
21a4f1e
Introduce ALS store for request data within middleware runtime
LauraBeatris 82f6fd2
Fetch dynamic keys from `currentUser`
LauraBeatris 4a4b66c
Call `clerkClient()` within tests
LauraBeatris 6d96f08
Rollback changes on `authMiddleware`
LauraBeatris b101927
Run handshake tests
LauraBeatris a0851a6
Add test for `signInUrl`
LauraBeatris aa18e35
Add test to resolve auth signature
LauraBeatris 22c6e45
Add test to call `clerkClient` on application runtime
LauraBeatris 068938c
Add test to call `clerkClient` on middleware runtime
LauraBeatris 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 hidden or 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
This file contains hidden or 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,101 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
|
|
||
| import type { Application } from '../models/application'; | ||
| import { appConfigs } from '../presets'; | ||
| import { createTestUtils } from '../testUtils'; | ||
|
|
||
| test.describe('dynamic keys @nextjs', () => { | ||
| test.describe.configure({ mode: 'parallel' }); | ||
| let app: Application; | ||
|
|
||
| test.beforeAll(async () => { | ||
| app = await appConfigs.next.appRouter | ||
| .clone() | ||
| .addFile( | ||
| 'src/middleware.ts', | ||
| () => `import { clerkClient, clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server' | ||
| import { NextResponse } from 'next/server' | ||
|
|
||
| const isProtectedRoute = createRouteMatcher(['/protected']); | ||
| const shouldFetchBapi = createRouteMatcher(['/fetch-bapi-from-middleware']); | ||
|
|
||
| export default clerkMiddleware(async (auth, request) => { | ||
| if (isProtectedRoute(request)) { | ||
| auth().protect(); | ||
| } | ||
|
|
||
| if (shouldFetchBapi(request)){ | ||
| const count = await clerkClient().users.getCount(); | ||
|
|
||
| if (count){ | ||
| return NextResponse.redirect(new URL('/users-count', request.url)) | ||
| } | ||
| } | ||
| }, { | ||
| secretKey: process.env.CLERK_DYNAMIC_SECRET_KEY, | ||
| signInUrl: '/foobar' | ||
| }); | ||
|
|
||
| export const config = { | ||
| matcher: ['/((?!.*\\\\..*|_next).*)', '/', '/(api|trpc)(.*)'], | ||
| };`, | ||
| ) | ||
| .addFile( | ||
| 'src/app/users-count/page.tsx', | ||
| () => `import { clerkClient } from '@clerk/nextjs/server' | ||
|
|
||
| export default async function Page(){ | ||
| const count = await clerkClient().users.getCount() | ||
|
|
||
| return <p>Users count: {count}</p> | ||
| } | ||
| `, | ||
| ) | ||
| .commit(); | ||
|
|
||
| await app.setup(); | ||
| await app.withEnv(appConfigs.envs.withDynamicKeys); | ||
| await app.dev(); | ||
| }); | ||
|
|
||
| test.afterAll(async () => { | ||
| await app.teardown(); | ||
| }); | ||
|
|
||
| test.afterEach(async ({ page, context }) => { | ||
| const u = createTestUtils({ app, page, context }); | ||
| await u.page.signOut(); | ||
| await u.page.context().clearCookies(); | ||
| }); | ||
|
|
||
| test('redirects to `signInUrl` on `auth().protect()`', async ({ page, context }) => { | ||
| const u = createTestUtils({ app, page, context }); | ||
|
|
||
| await u.page.goToStart(); | ||
|
|
||
| await u.po.expect.toBeSignedOut(); | ||
|
|
||
| await u.page.goToRelative('/protected'); | ||
|
|
||
| await u.page.waitForURL(/foobar/); | ||
| }); | ||
|
|
||
| test('resolves auth signature with `secretKey` on `auth().protect()`', async ({ page, context }) => { | ||
| const u = createTestUtils({ app, page, context }); | ||
| await u.page.goToRelative('/page-protected'); | ||
| await u.page.waitForURL(/foobar/); | ||
| }); | ||
|
|
||
| test('calls `clerkClient` with dynamic keys from application runtime', async ({ page, context }) => { | ||
| const u = createTestUtils({ app, page, context }); | ||
| await u.page.goToRelative('/users-count'); | ||
| await expect(u.page.getByText(/Users count/i)).toBeVisible(); | ||
| }); | ||
|
|
||
| test('calls `clerkClient` with dynamic keys from middleware runtime', async ({ page, context }) => { | ||
| const u = createTestUtils({ app, page, context }); | ||
| await u.page.goToRelative('/fetch-bapi-from-middleware'); | ||
| await u.page.waitForAppUrl('/users-count'); | ||
| await expect(u.page.getByText(/Users count/i)).toBeVisible(); | ||
| }); | ||
| }); |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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.
@nikosdouvlis I've went ahead and removed the
skipfrom handshake tests here.