-
Notifications
You must be signed in to change notification settings - Fork 460
fix(js): Coalesce concurrent first/second factor preparations #9225
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
Open
tmilewski
wants to merge
14
commits into
main
Choose a base branch
from
tom/fix-concurrent-second-factor-prepare
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
289cc55
Prevent duplicate concurrent sign-in factor preparations
tmilewski 9419cb5
fix(js): generalize prepare coalescing, key by resource id, cover sig…
tmilewski a19af24
chore: remove comments
tmilewski 7a411ca
fix(js): expire stalled coalesced preparations after a TTL
tmilewski 6168ff1
test(js): move sign-up prepare coalescing test into SignUp suite
tmilewski 1289a88
test(js): expand sign-up tests
tmilewski de0d200
fix(js): preserve resource serialization shape
tmilewski 85cdd45
fix(js): abort coalesced factor preparations when the stall TTL expires
tmilewski b967c52
fix(js): coalesce user profile verification preparations
tmilewski be5aa80
perf(js): lazily track coalesced preparations without timers
tmilewski 5df1f92
refactor(js): opt into preparation coalescing per call site
tmilewski 59bf726
fix(js): resolve coalescing build checks
tmilewski c4b4137
fix(js): preserve expired preparation requests
tmilewski 30e121c
test(js): expand verification coalescing coverage
tmilewski 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@clerk/clerk-js": patch | ||
| --- | ||
|
|
||
| Prevent overlapping verification preparations from triggering duplicate verification sends during sign-in, sign-up, and when verifying an email address, phone number, or web3 wallet from the user profile. |
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
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
123 changes: 123 additions & 0 deletions
123
packages/clerk-js/src/core/resources/__tests__/EmailAddress.test.ts
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,123 @@ | ||
| import type { EmailAddressJSON } from '@clerk/shared/types'; | ||
| import { createDeferredPromise } from '@clerk/shared/utils'; | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { BaseResource, EmailAddress } from '../internal'; | ||
|
|
||
| describe('EmailAddress', () => { | ||
| describe('prepareVerification', () => { | ||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('coalesces concurrent identical preparations', async () => { | ||
| const deferred = createDeferredPromise<any>(); | ||
| const mockFetch = vi.fn().mockReturnValue(deferred.promise); | ||
| // @ts-ignore | ||
| BaseResource._fetch = mockFetch; | ||
|
|
||
| const emailAddress = new EmailAddress({ id: 'email_123' } as EmailAddressJSON, '/me/email_addresses'); | ||
| const params = { strategy: 'email_code' as const }; | ||
|
|
||
| const first = emailAddress.prepareVerification(params); | ||
| const second = emailAddress.prepareVerification(params); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledTimes(1); | ||
|
|
||
| deferred.resolve({ | ||
| response: { id: 'email_123' }, | ||
| }); | ||
| await Promise.all([first, second]); | ||
| }); | ||
|
|
||
| it('does not coalesce preparations with different strategies or params', async () => { | ||
| const mockFetch = vi.fn().mockResolvedValue({ | ||
| response: { id: 'email_123' }, | ||
| }); | ||
| BaseResource._fetch = mockFetch; | ||
|
|
||
| const emailAddress = new EmailAddress({ id: 'email_123' } as EmailAddressJSON, '/me/email_addresses'); | ||
|
|
||
| await Promise.all([ | ||
| emailAddress.prepareVerification({ strategy: 'email_code' }), | ||
| emailAddress.prepareVerification({ strategy: 'email_link', redirectUrl: '/verify-one' }), | ||
| emailAddress.prepareVerification({ strategy: 'email_link', redirectUrl: '/verify-two' }), | ||
| ]); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| it('starts a new preparation after the previous request succeeds', async () => { | ||
| const mockFetch = vi.fn().mockResolvedValue({ | ||
| response: { id: 'email_123' }, | ||
| }); | ||
| // @ts-ignore | ||
| BaseResource._fetch = mockFetch; | ||
|
|
||
| const emailAddress = new EmailAddress({ id: 'email_123' } as EmailAddressJSON, '/me/email_addresses'); | ||
| const params = { strategy: 'email_code' as const }; | ||
|
|
||
| await emailAddress.prepareVerification(params); | ||
| await emailAddress.prepareVerification(params); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('starts a new preparation after the previous request fails', async () => { | ||
| const mockFetch = vi | ||
| .fn() | ||
| .mockRejectedValueOnce(new Error('prepare failed')) | ||
| .mockResolvedValueOnce({ | ||
| response: { id: 'email_123' }, | ||
| }); | ||
| BaseResource._fetch = mockFetch; | ||
|
|
||
| const emailAddress = new EmailAddress({ id: 'email_123' } as EmailAddressJSON, '/me/email_addresses'); | ||
| const params = { strategy: 'email_code' as const }; | ||
|
|
||
| await expect(emailAddress.prepareVerification(params)).rejects.toThrow('prepare failed'); | ||
| await emailAddress.prepareVerification(params); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('does not coalesce preparations across EmailAddress instances', async () => { | ||
| const deferred = createDeferredPromise<any>(); | ||
| const mockFetch = vi.fn().mockReturnValue(deferred.promise); | ||
| BaseResource._fetch = mockFetch; | ||
|
|
||
| const firstEmailAddress = new EmailAddress({ id: 'email_123' } as EmailAddressJSON, '/me/email_addresses'); | ||
| const secondEmailAddress = new EmailAddress({ id: 'email_123' } as EmailAddressJSON, '/me/email_addresses'); | ||
| const params = { strategy: 'email_code' as const }; | ||
|
|
||
| const first = firstEmailAddress.prepareVerification(params); | ||
| const second = secondEmailAddress.prepareVerification(params); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledTimes(2); | ||
|
|
||
| deferred.resolve({ | ||
| response: { id: 'email_123' }, | ||
| }); | ||
| await Promise.all([first, second]); | ||
| }); | ||
|
|
||
| it('does not coalesce concurrent verification attempts', async () => { | ||
| const deferred = createDeferredPromise<any>(); | ||
| const mockFetch = vi.fn().mockReturnValue(deferred.promise); | ||
| BaseResource._fetch = mockFetch; | ||
|
|
||
| const emailAddress = new EmailAddress({ id: 'email_123' } as EmailAddressJSON, '/me/email_addresses'); | ||
| const params = { code: '123456' }; | ||
|
|
||
| const first = emailAddress.attemptVerification(params); | ||
| const second = emailAddress.attemptVerification(params); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledTimes(2); | ||
|
|
||
| deferred.resolve({ | ||
| response: { id: 'email_123' }, | ||
| }); | ||
| await Promise.all([first, second]); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.