Skip to content

Commit 6f4816c

Browse files
committed
fix(mosaic): format phone numbers in the sign-in code step
1 parent 584a369 commit 6f4816c

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

‎packages/mosaic/src/features/sign-in/__tests__/sign-in.utils.test.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ describe('code step copy', () => {
7676
expect(codeCopy(step, m, 'Acme').description).toContain('jane@acme.com');
7777
});
7878

79+
it('formats a phone number named in the description', () => {
80+
const step = { kind: 'code', purpose: 'second-factor', channel: 'phone', identifier: '+15555550123' } as const;
81+
82+
expect(codeCopy(step, m, 'Acme').description).toBe('Enter the verification code sent to +1 (555) 555-0123');
83+
});
84+
85+
it('leaves a masked phone number as FAPI sent it', () => {
86+
const step = { kind: 'code', purpose: 'reset-password', channel: 'phone', identifier: '+1******0123' } as const;
87+
88+
expect(codeCopy(step, m, 'Acme').description).toBe('First, enter the code sent to your phone +1******0123');
89+
});
90+
7991
it('warns on a new device and on an SSO bypass', () => {
8092
expect(codeNotice({ kind: 'code', purpose: 'client-trust', channel: 'email' }, m)).toBe(m.clientTrust.notice);
8193
expect(codeNotice({ kind: 'code', purpose: 'sso-bypass', channel: 'email' }, m)).toBe(m.ssoBypass.notice);
@@ -86,7 +98,7 @@ describe('code step copy', () => {
8698
const step = { kind: 'code', purpose: 'verify-phone', channel: 'phone', identifier: '+1 555 555 0123' } as const;
8799

88100
expect(codeCopy(step, m, 'Acme').title).toBe('Verify your phone');
89-
expect(codeIdentity(step, m).identifier).toBe('+1 555 555 0123');
101+
expect(codeIdentity(step, m).identifier).toBe('+1 (555) 555-0123');
90102
expect(
91103
codeSecondaryAction(step, m, { onCancel: () => undefined, onBack: () => undefined }).onClick,
92104
).toBeUndefined();

‎packages/mosaic/src/features/sign-in/sign-in.utils.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { stringToFormattedPhoneString } from '@clerk/shared/phone';
2+
13
import type { MosaicMessages } from '../../localization';
24
import { fill } from '../../localization';
35
import type { SignInStep } from './sign-in.types';
@@ -38,7 +40,7 @@ export function codeCopy(
3840
m: Messages,
3941
applicationName: string,
4042
): { title: string; description: string; fieldLabel: string } {
41-
const identifier = step.identifier ?? '';
43+
const identifier = displayIdentifier(step) ?? '';
4244
if (step.purpose === 'reset-password') {
4345
const description =
4446
step.channel === 'phone' ? m.resetPasswordCode.descriptionPhone : m.resetPasswordCode.descriptionEmail;
@@ -110,9 +112,17 @@ function sentToIdentifier(step: CodeStepData): boolean {
110112
return Boolean(step.identifier) && !identifierEditable(step);
111113
}
112114

115+
function displayIdentifier(step: CodeStepData): string | undefined {
116+
const { identifier } = step;
117+
if (step.channel !== 'phone' || !identifier || identifier.includes('*')) {
118+
return identifier;
119+
}
120+
return stringToFormattedPhoneString(identifier);
121+
}
122+
113123
export function codeIdentity(step: CodeStepData, m: Messages): { identifier?: string; editLabel: string } {
114124
return {
115-
identifier: step.channel !== 'totp' && identifierEditable(step) ? step.identifier : undefined,
125+
identifier: step.channel !== 'totp' && identifierEditable(step) ? displayIdentifier(step) : undefined,
116126
editLabel: step.channel === 'phone' ? m.editPhoneNumber : m.editIdentifier,
117127
};
118128
}

0 commit comments

Comments
 (0)