-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nextjs,backend,integration): Introduce dynamic keys from `clerkM…
…iddleware` (#3525)
- Loading branch information
1 parent
168607a
commit f1847b7
Showing
21 changed files
with
385 additions
and
110 deletions.
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,11 @@ | ||
--- | ||
'@clerk/backend': minor | ||
'@clerk/nextjs': minor | ||
--- | ||
|
||
Introduces dynamic keys from `clerkMiddleware`, allowing access by server-side helpers like `auth`. Keys such as `signUpUrl`, `signInUrl`, `publishableKey` and `secretKey` are securely encrypted using AES algorithm. | ||
|
||
- When providing `secretKey`, `CLERK_ENCRYPTION_KEY` is required as the encryption key. If `secretKey` is not provided, `CLERK_SECRET_KEY` is used by default. | ||
- `clerkClient` from `@clerk/nextjs` should now be called as a function, and its singleton form is deprecated. This change allows the Clerk backend client to read keys from the current request, which is necessary to support dynamic keys. | ||
|
||
For more information, refer to the documentation: https://clerk.com/docs/references/nextjs/clerk-middleware#dynamic-keys |
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
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
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
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,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 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 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 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 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 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 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 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.