-
Notifications
You must be signed in to change notification settings - Fork 465
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
base: main
Are you sure you want to change the base?
Changes from all commits
de9b78e
418ac83
7a76ac2
ed13b06
09bfef2
2cd115a
e34957f
1151f46
c86b9f7
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,12 @@ | ||
| --- | ||
| '@clerk/clerk-js': minor | ||
| '@clerk/shared': minor | ||
| --- | ||
|
|
||
| Acquire an optional Protect session token and attach it to sign-in and sign-up requests. | ||
|
|
||
| On instances whose loader config references the new `{cid}` / `{pid}` / `{rid}` / `{instance_id}` / `{sdkver}` placeholders, Clerk mints an opaque, random correlation id and substitutes it into the loader's attributes and `textContent`. The loader is served with a signed session token, which is taken up once per browser session — shared across tabs under a lock rather than acquired once per tab — and travels alongside the correlation id and an acquisition status in the form-encoded body of sign-in and sign-up requests. | ||
|
|
||
| Acquisition can never block or fail a sign-in: it is bounded by a deadline, and when no token can be obtained a status (`no_token`, `timeout`, `script_error`, `fetch_error`, `unsupported`, `http_<n>`) travels in its place and the request proceeds unchanged. Only the loader carrying the correlation id is governed by the shared token; any other configured loader is still applied on every page load. Nothing is stored in the browser unless a loader references `{cid}`, `{pid}` or `{rid}`. | ||
|
|
||
| `ProtectLoader` gains two optional fields: `tokenTimeoutMs`, and `tokenUrl` for instances that opt into fetching the token from a dedicated endpoint instead of taking the one served with the loader. | ||
|
Comment on lines
+6
to
+12
Member
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. This describes the technical implementation, but it doesn't really explain to an end user reading the release changelog what it does. I'd favor a two sentence summary of the capabilities this gives instead, no need for this level of technical detail here. |
||
| 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); | ||
|
Comment on lines
+23
to
+31
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. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Type the FAPI mock options. Lines 23 and 29 use As per coding guidelines, “Avoid 🤖 Prompt for AI AgentsSource: Coding guidelines |
||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| 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 |
||
Uh oh!
There was an error while loading. Please reload this page.