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-js): Do not treat phone_number as optional if used as identifier#5992
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
Merged
brkalow
merged 2 commits into
main
from
dylan/sdki-1020-sign-up-not-being-immediately-submitted-when-phone-number-isMay 28, 2025
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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 | ||
| --- | ||
| Fix issue where the combined flow wouldn't trigger if a phone number was used as an identifier while set as an optional field. |
177 changes: 177 additions & 0 deletions
177 packages/clerk-js/src/ui/components/SignIn/__tests__/handleCombinedFlowTransfer.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,177 @@ | ||
| import type { LoadedClerk, SignUpResource } from '@clerk/types'; | ||
| import { handleCombinedFlowTransfer, hasOptionalFields } from '../handleCombinedFlowTransfer'; | ||
| // eslint-disable-next-line no-var -- Jest hoists mock calls to the top of the file, so var is needed. | ||
| var mockCompleteSignUpFlow: jest.Mock; | ||
| jest.mock('../lazy-sign-up', () => { | ||
| mockCompleteSignUpFlow = jest.fn(); | ||
| return { | ||
| lazyCompleteSignUpFlow: () => { | ||
| return Promise.resolve(mockCompleteSignUpFlow); | ||
| }, | ||
| }; | ||
| }); | ||
| const mockNavigate = jest.fn(); | ||
| const mockHandleError = jest.fn(); | ||
| describe('handleCombinedFlowTransfer', () => { | ||
| beforeEach(() => { | ||
| jest.resetAllMocks(); | ||
| }); | ||
| it('should call completeSignUpFlow', async () => { | ||
| const mockClerk = { | ||
| client: { | ||
| signUp: { | ||
| create: jest.fn().mockResolvedValue({}), | ||
| optionalFields: [], | ||
| }, | ||
| }, | ||
| }; | ||
| await handleCombinedFlowTransfer({ | ||
| identifierAttribute: 'emailAddress', | ||
| identifierValue: 'test@test.com', | ||
| signUpMode: 'public', | ||
| navigate: mockNavigate, | ||
| handleError: mockHandleError, | ||
| clerk: mockClerk as unknown as LoadedClerk, | ||
| afterSignUpUrl: 'https://test.com', | ||
| passwordEnabled: false, | ||
| }); | ||
| expect(mockCompleteSignUpFlow).toHaveBeenCalled(); | ||
| }); | ||
| it('should call completeSignUpFlow with phone number if phone number is optional field.', async () => { | ||
| const mockClerk = { | ||
| client: { | ||
| signUp: { | ||
| create: jest.fn().mockImplementation((...args) => Promise.resolve(args)), | ||
| optionalFields: ['phone_number'], | ||
| }, | ||
| }, | ||
| }; | ||
| await handleCombinedFlowTransfer({ | ||
| identifierAttribute: 'phoneNumber', | ||
| identifierValue: '+1234567890', | ||
| signUpMode: 'public', | ||
| navigate: mockNavigate, | ||
| handleError: mockHandleError, | ||
| clerk: mockClerk as unknown as LoadedClerk, | ||
| afterSignUpUrl: 'https://test.com', | ||
| passwordEnabled: false, | ||
| }); | ||
| expect(mockNavigate).not.toHaveBeenCalled(); | ||
| expect(mockClerk.client.signUp.create).toHaveBeenCalled(); | ||
| expect(mockCompleteSignUpFlow).toHaveBeenCalled(); | ||
| }); | ||
| it('should call navigate if password is enabled', async () => { | ||
| const mockClerk = { | ||
| client: { | ||
| signUp: { | ||
| create: jest.fn().mockImplementation((...args) => Promise.resolve(args)), | ||
| optionalFields: ['password'], | ||
| }, | ||
| }, | ||
| }; | ||
| await handleCombinedFlowTransfer({ | ||
| identifierAttribute: 'phoneNumber', | ||
| identifierValue: '+1234567890', | ||
| signUpMode: 'public', | ||
| navigate: mockNavigate, | ||
| handleError: mockHandleError, | ||
| clerk: mockClerk as unknown as LoadedClerk, | ||
| afterSignUpUrl: 'https://test.com', | ||
| passwordEnabled: true, | ||
| }); | ||
| expect(mockNavigate).toHaveBeenCalled(); | ||
| expect(mockClerk.client.signUp.create).not.toHaveBeenCalled(); | ||
| expect(mockCompleteSignUpFlow).not.toHaveBeenCalled(); | ||
| }); | ||
| it('should call navigate if identifier is username', async () => { | ||
| const mockClerk = { | ||
| client: { | ||
| signUp: { | ||
| create: jest.fn().mockImplementation((...args) => Promise.resolve(args)), | ||
| optionalFields: [], | ||
| }, | ||
| }, | ||
| }; | ||
| await handleCombinedFlowTransfer({ | ||
| identifierAttribute: 'username', | ||
| identifierValue: 'test', | ||
| signUpMode: 'public', | ||
| navigate: mockNavigate, | ||
| handleError: mockHandleError, | ||
| clerk: mockClerk as unknown as LoadedClerk, | ||
| afterSignUpUrl: 'https://test.com', | ||
| passwordEnabled: false, | ||
| }); | ||
| expect(mockNavigate).toHaveBeenCalled(); | ||
| expect(mockClerk.client.signUp.create).not.toHaveBeenCalled(); | ||
| expect(mockCompleteSignUpFlow).not.toHaveBeenCalled(); | ||
| }); | ||
| it('should call navigate if first_name is optional', async () => { | ||
| const mockClerk = { | ||
| client: { | ||
| signUp: { | ||
| create: jest.fn().mockImplementation((...args) => Promise.resolve(args)), | ||
| optionalFields: ['first_name'], | ||
| }, | ||
| }, | ||
| }; | ||
| await handleCombinedFlowTransfer({ | ||
| identifierAttribute: 'emailAddress', | ||
| identifierValue: 'test@test.com', | ||
| signUpMode: 'public', | ||
| navigate: mockNavigate, | ||
| handleError: mockHandleError, | ||
| clerk: mockClerk as unknown as LoadedClerk, | ||
| afterSignUpUrl: 'https://test.com', | ||
| passwordEnabled: false, | ||
| }); | ||
| expect(mockNavigate).toHaveBeenCalled(); | ||
| expect(mockClerk.client.signUp.create).not.toHaveBeenCalled(); | ||
| expect(mockCompleteSignUpFlow).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| describe('hasOptionalFields', () => { | ||
| it('should return true if there are optional fields', () => { | ||
| const signUp = { | ||
| optionalFields: ['legal_accepted'], | ||
| } as unknown as SignUpResource; | ||
| expect(hasOptionalFields(signUp, 'phoneNumber')).toBe(true); | ||
| }); | ||
| it('should return false if the identifier attribute is phoneNumber and the optional field is phone_number', () => { | ||
| const signUp = { | ||
| optionalFields: ['phone_number'], | ||
| } as unknown as SignUpResource; | ||
| expect(hasOptionalFields(signUp, 'phoneNumber')).toBe(false); | ||
| }); | ||
| it('should return false if there are no optional fields', () => { | ||
| const signUp = { | ||
| optionalFields: [], | ||
| } as unknown as SignUpResource; | ||
| expect(hasOptionalFields(signUp, 'phoneNumber')).toBe(false); | ||
| }); | ||
| }); |
34 changes: 24 additions & 10 deletions
34 packages/clerk-js/src/ui/components/SignIn/handleCombinedFlowTransfer.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 |
|---|---|---|
| @@ -69,7 +69,7 @@ export function handleCombinedFlowTransfer({ | ||
| // inform us if the instance is eligible for moving directly to verification. | ||
| if ( | ||
| !passwordEnabled && | ||
| !hasOptionalFields(clerk.client.signUp) && | ||
| !hasOptionalFields(clerk.client.signUp, identifierAttribute) && | ||
| (identifierAttribute === 'emailAddress' || identifierAttribute === 'phoneNumber') | ||
| ) { | ||
| return clerk.client.signUp | ||
| @@ -95,14 +95,28 @@ export function handleCombinedFlowTransfer({ | ||
| return navigate(`create`, { searchParams: paramsToForward }); | ||
| } | ||
| function hasOptionalFields(signUp: SignUpResource) { | ||
| const filteredFields = signUp.optionalFields.filter( | ||
| field => | ||
| !field.startsWith('oauth_') && | ||
| !field.startsWith('web3_') && | ||
| field !== 'password' && | ||
| field !== 'enterprise_sso' && | ||
| field !== 'saml', | ||
| ); | ||
| export function hasOptionalFields( | ||
| signUp: SignUpResource, | ||
| identifierAttribute: 'emailAddress' | 'phoneNumber' | 'username', | ||
| ) { | ||
| const filteredFields = signUp.optionalFields.filter(field => { | ||
| // OAuth, Web3, and SAML fields, while optional, are not relevant once sign up has been initiated with an identifier. | ||
| if (field.startsWith('oauth_') || field.startsWith('web3_') || ['enterprise_sso', 'saml'].includes(field)) { | ||
| return false; | ||
| } | ||
| // We already check for whether password is enabled, so we don't consider it an optional field. | ||
| if (field === 'password') { | ||
| return false; | ||
| } | ||
dstaley marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // If a phone number is used as the identifier, we don't need to consider the phone_number field. | ||
| if (identifierAttribute === 'phoneNumber' && field === 'phone_number') { | ||
| return false; | ||
| } | ||
| return true; | ||
| }); | ||
| return filteredFields.length > 0; | ||
| } | ||
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.