-
Notifications
You must be signed in to change notification settings - Fork 464
feat(js): attach an optional server-configured session token to sign-in #9299
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
Changes from all commits
de9b78e
418ac83
7a76ac2
ed13b06
09bfef2
2cd115a
e34957f
1151f46
c86b9f7
be077b6
d490477
66f7fc6
09db0b3
8cace84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@clerk/clerk-js': minor | ||
| '@clerk/shared': minor | ||
| --- | ||
|
|
||
| Internal improvements to Clerk Protect. No action is required, and instances that do not use Protect are unaffected. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import type { ProtectAssertion } from '@clerk/shared/types'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { Clerk } from '../clerk'; | ||
|
|
||
| /** | ||
| * Two independent Protect features feed the single `getProtectParams` hook the FAPI client calls: | ||
| * the application-supplied assertion, and the server-configured session token. They are wired in | ||
| * the same expression, so collapsing it to either one alone still compiles, still type-checks, and | ||
| * silently stops sending the other's params — a degradation with nothing to see. These tests pin | ||
| * the hook to the union. | ||
| */ | ||
|
|
||
| const getRequestParams = vi.fn(); | ||
|
|
||
| vi.mock('../protect', () => ({ | ||
| Protect: class { | ||
| load = vi.fn(); | ||
| getRequestParams = getRequestParams; | ||
| }, | ||
| })); | ||
|
|
||
| const { capturedOptions } = vi.hoisted(() => ({ capturedOptions: { current: undefined as any } })); | ||
|
|
||
| vi.mock('../fapiClient', async importOriginal => { | ||
| const actual = await importOriginal<typeof import('../fapiClient')>(); | ||
| return { | ||
| ...actual, | ||
| createFapiClient: (options: any) => { | ||
| capturedOptions.current = options; | ||
| return actual.createFapiClient(options); | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| const productionPublishableKey = 'pk_live_Y2xlcmsuYWJjZWYuMTIzNDUucHJvZC5sY2xjbGVyay5jb20k'; | ||
|
|
||
| const sessionParams = { __clerk_protect_token: 'v1.payload.mac', __clerk_protect_status: 'ok' }; | ||
| const assertionParams = { __clerk_protect_assertion: 'token-abc' }; | ||
|
|
||
| /** The hook a freshly constructed Clerk handed to the FAPI client. */ | ||
| const hookFor = (assertion?: ProtectAssertion) => { | ||
| const clerk = new Clerk(productionPublishableKey); | ||
| if (assertion !== undefined) { | ||
| clerk.setProtectAssertion(assertion); | ||
| } | ||
| return capturedOptions.current.getProtectParams as () => Promise<Record<string, string | undefined> | undefined>; | ||
| }; | ||
|
|
||
| describe('Clerk getProtectParams', () => { | ||
| beforeEach(() => { | ||
| getRequestParams.mockReset(); | ||
| capturedOptions.current = undefined; | ||
| }); | ||
|
|
||
| it('is wired into the FAPI client', () => { | ||
| expect(hookFor()).toBeTypeOf('function'); | ||
| }); | ||
|
|
||
| it('unions the assertion and the session token', async () => { | ||
| getRequestParams.mockResolvedValue(sessionParams); | ||
|
|
||
| await expect(hookFor('token-abc')()).resolves.toEqual({ ...assertionParams, ...sessionParams }); | ||
| }); | ||
|
|
||
| it('sends the session token when no assertion is configured', async () => { | ||
| getRequestParams.mockResolvedValue(sessionParams); | ||
|
|
||
| await expect(hookFor()()).resolves.toEqual(sessionParams); | ||
| }); | ||
|
|
||
| it('sends the assertion when the session contributes nothing', async () => { | ||
| getRequestParams.mockResolvedValue(undefined); | ||
|
|
||
| await expect(hookFor('token-abc')()).resolves.toEqual(assertionParams); | ||
| }); | ||
|
|
||
| // Returning `{}` would make every sign-in body differ from what it was before the feature existed. | ||
| it('resolves to undefined when neither contributes anything', async () => { | ||
| getRequestParams.mockResolvedValue(undefined); | ||
|
|
||
| await expect(hookFor()()).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| // Neither feature may take the other down with it. | ||
| it('keeps the session token when the assertion resolver throws', async () => { | ||
| getRequestParams.mockResolvedValue(sessionParams); | ||
|
|
||
| await expect( | ||
| hookFor(() => { | ||
| throw new Error('boom'); | ||
| })(), | ||
| ).resolves.toEqual(sessionParams); | ||
| }); | ||
|
|
||
| it('keeps the assertion when acquiring the session token rejects', async () => { | ||
| getRequestParams.mockRejectedValue(new DOMException('storage is blocked', 'SecurityError')); | ||
|
|
||
| await expect(hookFor('token-abc')()).resolves.toEqual(assertionParams); | ||
| }); | ||
| }); | ||
|
Comment on lines
+50
to
+101
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. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Test explicit assertion clearing. Add a test that loads Clerk with As per coding guidelines, “Unit tests are required for all new functionality” and tests must verify edge cases. 🤖 Prompt for AI AgentsSource: Coding guidelines |
||
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Type the FAPI mock options.
Lines 23 and 29 use
any, so acreateFapiClientcontract change can bypass this integration test. Derive the captured value and mock parameter fromParameters<typeof createFapiClient>[0].As per coding guidelines, “Avoid
anytype” and “Noanytypes without justification in code review.”🤖 Prompt for AI Agents
Source: Coding guidelines