Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 470
fix(clerk-sdk-node): Inherit verifyToken options from clerkClient#3296
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
9a7208b558dab61dec0219d852adcabc23ab9c92b81afcdcd5782b00File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --- | ||
| '@clerk/clerk-sdk-node': patch | ||
| '@clerk/backend': patch | ||
| --- | ||
| Inherit verifyToken options from clerkClient. | ||
| The below code now works as expected: (requires CLERK_SECRET_KEY env var to have been set) | ||
| ```ts | ||
| import { clerkClient } from "@clerk/clerk-sdk-node"; | ||
| // Use the default settings from the already instanciated clerkClient | ||
| clerkClient.verifyToken(token) | ||
| // or provide overrides the options | ||
| clerkClient.verifyToken(token, { | ||
| secretKey: 'xxxx' | ||
| }) | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| const verifyTokenMock = jest.fn(); | ||
| jest.mock('@clerk/backend', () => ({ | ||
| ...jest.requireActual('@clerk/backend'), | ||
| verifyToken: verifyTokenMock, | ||
| })); | ||
| import { createClerkClient } from '../clerkClient'; | ||
| afterEach(() => { | ||
| verifyTokenMock.mockReset(); | ||
| }); | ||
| describe('verifyToken', () => { | ||
| it('correctly use the predefined options of clerkClient', async () => { | ||
| const clerkClient = createClerkClient({ | ||
| secretKey: '123', | ||
| jwtKey: '456', | ||
| }); | ||
| await clerkClient.verifyToken('token'); | ||
| expect(verifyTokenMock).toHaveBeenCalledWith( | ||
| 'token', | ||
| expect.objectContaining({ | ||
| secretKey: '123', | ||
| jwtKey: '456', | ||
| }), | ||
| ); | ||
| }); | ||
| it('correctly use the passed options in verifyToken', async () => { | ||
| const clerkClient = createClerkClient({ | ||
| secretKey: '123', | ||
| jwtKey: '456', | ||
| }); | ||
| await clerkClient.verifyToken('token', { | ||
| secretKey: '987', | ||
| }); | ||
| expect(verifyTokenMock).toHaveBeenCalledWith( | ||
| 'token', | ||
| expect.objectContaining({ | ||
| secretKey: '987', | ||
| jwtKey: '456', | ||
| }), | ||
| ); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,25 @@ | ||
| import type { ClerkOptions } from '@clerk/backend'; | ||
| import { createClerkClient as _createClerkClient, verifyToken } from '@clerk/backend'; | ||
| import type { ClerkOptions, VerifyTokenOptions } from '@clerk/backend'; | ||
| import { createClerkClient as _createClerkClient, verifyToken as _verifyToken } from '@clerk/backend'; | ||
| import { createClerkExpressRequireAuth } from './clerkExpressRequireAuth'; | ||
| import { createClerkExpressWithAuth } from './clerkExpressWithAuth'; | ||
| import { loadApiEnv, loadClientEnv } from './utils'; | ||
| type MakeOptionalSecondArgument<T> = T extends (a: string, b: infer U) => infer R ? (a: string, b?: U) => R : never; | ||
| type VerifyTokenWithOptionalSecondArgument = MakeOptionalSecondArgument<typeof _verifyToken>; | ||
| type ClerkClient = ReturnType<typeof _createClerkClient> & { | ||
| expressWithAuth: ReturnType<typeof createClerkExpressWithAuth>; | ||
| expressRequireAuth: ReturnType<typeof createClerkExpressRequireAuth>; | ||
| verifyToken: typeof verifyToken; | ||
| verifyToken: VerifyTokenWithOptionalSecondArgument; | ||
| }; | ||
| const buildVerifyToken = (params: VerifyTokenOptions) => { | ||
| return (...args: Parameters<VerifyTokenWithOptionalSecondArgument>) => | ||
| _verifyToken(args[0], { | ||
| ...params, | ||
| ...args[1], | ||
Contributor 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. 🔧 Could you add 2 tests (with options, without options) to verify that everything works as expected? | ||
| }); | ||
| }; | ||
| /** | ||
| @@ -24,7 +35,7 @@ export function createClerkClient(options: ClerkOptions): ClerkClient { | ||
| return Object.assign(clerkClient, { | ||
| expressWithAuth, | ||
| expressRequireAuth, | ||
| verifyToken, | ||
| verifyToken: buildVerifyToken(options), | ||
| }); | ||
| } | ||
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.
@dimkl fyi, Just cleaning this up