Skip to content

Commit f4cc97e

Browse files
docs(swingset): demonstrate SMS setup and verification
1 parent e511b7d commit f4cc97e

3 files changed

Lines changed: 204 additions & 1 deletion

File tree

‎packages/swingset/src/lib/registry.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ import {
266266
meta as userProfileMfaSectionMeta,
267267
ReadOnly as UserProfileMfaSectionReadOnly,
268268
Removal as UserProfileMfaSectionRemoval,
269+
SmsSetup as UserProfileMfaSectionSmsSetup,
269270
} from '../stories/user-profile-mfa-section.stories';
270271
import {
271272
CreationUnavailable as UserProfilePasskeysSectionCreationUnavailable,
@@ -579,6 +580,7 @@ const userProfilePasskeysSectionModule: StoryModule = {
579580
const userProfileMfaSectionModule: StoryModule = {
580581
AddMethod: UserProfileMfaSectionAddMethod,
581582
AuthenticatorSetup: UserProfileMfaSectionAuthenticatorSetup,
583+
SmsSetup: UserProfileMfaSectionSmsSetup,
582584
meta: userProfileMfaSectionMeta,
583585
Default: UserProfileMfaSectionDefault,
584586
Empty: UserProfileMfaSectionEmpty,

‎packages/swingset/src/stories/user-profile-mfa-section.mdx‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,33 @@ Each method has an `id`, a `type` (`sms`, `authenticator`, or `backup-codes`), a
6464
| `isPending` | `boolean` | `false` | Disables code entry and Cancel; Verify shows progress and blocks repeat submissions. |
6565
| `errorMessage` | `string` | — | Feedback associated with the verification field. |
6666

67+
### SMS dialog
68+
69+
`UserProfileAddSmsDialog` combines number selection with the shared phone-entry and verification steps.
70+
71+
| Prop | Type | Default | Description |
72+
| ------------------------- | ------------------------------------------------ | ------------ | ------------------------------------------------------------------------------------------------------- |
73+
| `open` | `boolean` | — (required) | Whether the dialog is open. |
74+
| `onOpenChange` | `(open: boolean) => void` | — (required) | Receives open and dismiss requests. |
75+
| `trigger` | `DialogTriggerProps['render']` | — | Trigger that receives focus after dismissal. |
76+
| `step` | `'select' \| 'phone' \| 'verify'` | — (required) | Active screen. |
77+
| `direction` | `1 \| -1` | `1` | Forward or backward transition. |
78+
| `phoneNumbers` | `readonly { id: string; phoneNumber: string }[]` | — (required) | Eligible existing numbers, in display order. |
79+
| `selectedPhoneId` | `string` | — (required) | Selected number ID; an empty string disables Continue. |
80+
| `onSelectedPhoneIdChange` | `(id: string) => void` | — (required) | Receives a Select choice. |
81+
| `onAddPhone` | `() => void` | — (required) | Requests phone entry. |
82+
| `onBack` | `() => void` | — (required) | Requests the previous screen. |
83+
| `phoneNumber` | `string` | — (required) | Phone-entry value or the number being verified. |
84+
| `onPhoneNumberChange` | `(value: string) => void` | — (required) | Receives phone edits. |
85+
| `code` | `string` | — (required) | Current verification code. |
86+
| `onCodeChange` | `(value: string) => void` | — (required) | Receives code edits. |
87+
| `onSubmit` | `(code?: string) => void` | — (required) | Submits the active step. OTP completion supplies the completed code; form submission uses caller state. |
88+
| `onResend` | `() => void` | — (required) | Requests another verification code. |
89+
| `isPending` | `boolean` | `false` | Blocks edits and navigation and shows submit progress. |
90+
| `errorMessage` | `string` | — | Error associated with the active field. |
91+
| `isResending` | `boolean` | `false` | Blocks code entry, verification, resend, and Back while sending. |
92+
| `resendSeconds` | `number` | `0` | Seconds remaining before resend becomes available. |
93+
6794
## Usage
6895

6996
The caller decides whether to mount the section from the instance's second-factor configuration, even when existing methods are present. When mounted, the section stays visible with no methods or callbacks. The caller also prepares the method order, default indicators, and removal permissions, including restrictions when MFA is required. Backup-code rows only offer regeneration.
@@ -97,6 +124,29 @@ The caller owns the code, pending state, error feedback, and success closure. Cl
97124
]}
98125
/>
99126

127+
### Set up SMS verification
128+
129+
Choose an existing number with Select or add a new number. New numbers use the same phone-entry and code-verification steps as Add phone number. Back returns to the previous screen; returning from verification to phone entry keeps the entered number.
130+
131+
In this example, the first existing number is already verified and enables SMS directly. The second existing number and newly entered numbers require a code. Enter any six digits to see an initial error, then choose Verify to retry successfully. Resend shows a countdown and pending feedback. Closing and reopening resets the example. All requests are simulated; no text messages are sent or account changes made.
132+
133+
The caller supplies eligible numbers and owns selection, navigation, sending codes, enabling SMS, and success closure. It decides whether an existing number needs verification and may open directly on the phone step when there are no existing numbers. The view renders the supplied step and reports actions. Keep dismissal blocked during pending requests, clear errors on edits or retries, and preserve the phone number when going Back.
134+
135+
<Story
136+
name='SmsSetup'
137+
storyModule={Stories}
138+
composition={[
139+
{ name: 'Dialog', href: '/components/dialog', layer: 'Components' },
140+
{ name: 'Card', href: '/components/card', layer: 'Components' },
141+
{ name: 'Flow', href: '/components/flow', layer: 'Components' },
142+
{ name: 'Button', href: '/components/button', layer: 'Components' },
143+
{ name: 'Field', href: '/components/field', layer: 'Components' },
144+
{ name: 'Select', href: '/components/select', layer: 'Components' },
145+
{ name: 'PhoneInput', href: '/components/phone-input', layer: 'Components' },
146+
{ name: 'Otp', href: '/components/otp', layer: 'Components' },
147+
]}
148+
/>
149+
100150
### Confirm removal and retry
101151

102152
Choose Remove method from either row. Cancel keeps the method and returns focus to its menu. The first confirmed removal in this example fails after a short delay; retry completes it. Removing both methods leaves the empty section visible.

‎packages/swingset/src/stories/user-profile-mfa-section.stories.tsx‎

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { Button } from '@clerk/ui/mosaic/components/button';
22
import { Text } from '@clerk/ui/mosaic/components/text';
33
import { UserProfileAddAuthenticatorDialog } from '@clerk/ui/mosaic/features/user-profile/user-profile-add-authenticator.dialog';
4+
import type { UserProfileAddSmsDialogProps } from '@clerk/ui/mosaic/features/user-profile/user-profile-add-sms.dialog';
5+
import { UserProfileAddSmsDialog } from '@clerk/ui/mosaic/features/user-profile/user-profile-add-sms.dialog';
46
import type { UserProfileMfaMethod } from '@clerk/ui/mosaic/features/user-profile/user-profile-mfa-section.view';
57
import { UserProfileMfaSectionView } from '@clerk/ui/mosaic/features/user-profile/user-profile-mfa-section.view';
6-
import { useRef, useState } from 'react';
8+
import { useEffect, useRef, useState } from 'react';
79

810
import type { StoryMeta } from '@/lib/types';
911

@@ -151,6 +153,155 @@ export function AuthenticatorSetup() {
151153
);
152154
}
153155

156+
export function SmsSetup() {
157+
const phoneNumbers = [
158+
{ id: 'personal', phoneNumber: '+18015550100', verified: true },
159+
{ id: 'work', phoneNumber: '+18015550200', verified: false },
160+
];
161+
const [open, setOpen] = useState(false);
162+
const [step, setStep] = useState<UserProfileAddSmsDialogProps['step']>('select');
163+
const [direction, setDirection] = useState<1 | -1>(1);
164+
const [selectedPhoneId, setSelectedPhoneId] = useState('personal');
165+
const [phoneNumber, setPhoneNumber] = useState('');
166+
const [code, setCode] = useState('');
167+
const [verifyFrom, setVerifyFrom] = useState<'select' | 'phone'>('select');
168+
const [isPending, setIsPending] = useState(false);
169+
const [isResending, setIsResending] = useState(false);
170+
const [resendSeconds, setResendSeconds] = useState(0);
171+
const [errorMessage, setErrorMessage] = useState<string>();
172+
const [enabledPhone, setEnabledPhone] = useState('');
173+
const hasFailed = useRef(false);
174+
175+
useEffect(() => {
176+
if (resendSeconds <= 0) {
177+
return;
178+
}
179+
const timeout = setTimeout(() => setResendSeconds(seconds => seconds - 1), 1000);
180+
return () => clearTimeout(timeout);
181+
}, [resendSeconds]);
182+
183+
const submit = async () => {
184+
if (isPending || isResending) {
185+
return;
186+
}
187+
setIsPending(true);
188+
setErrorMessage(undefined);
189+
await new Promise(resolve => setTimeout(resolve, 800));
190+
setIsPending(false);
191+
if (step === 'verify') {
192+
if (!hasFailed.current) {
193+
hasFailed.current = true;
194+
setErrorMessage('That code could not be verified. Please try again.');
195+
return;
196+
}
197+
setEnabledPhone(phoneNumber);
198+
setOpen(false);
199+
setResendSeconds(0);
200+
return;
201+
}
202+
if (step === 'select') {
203+
const phone = phoneNumbers.find(number => number.id === selectedPhoneId);
204+
if (!phone) {
205+
return;
206+
}
207+
if (phone.verified) {
208+
setEnabledPhone(phone.phoneNumber);
209+
setOpen(false);
210+
return;
211+
}
212+
setPhoneNumber(phone.phoneNumber);
213+
}
214+
setVerifyFrom(step);
215+
setCode('');
216+
setResendSeconds(12);
217+
setDirection(1);
218+
setStep('verify');
219+
};
220+
221+
const resend = async () => {
222+
if (isPending || isResending || resendSeconds > 0) {
223+
return;
224+
}
225+
setIsResending(true);
226+
setErrorMessage(undefined);
227+
setCode('');
228+
await new Promise(resolve => setTimeout(resolve, 800));
229+
setIsResending(false);
230+
setResendSeconds(12);
231+
};
232+
233+
return (
234+
<div className='flex flex-col items-center gap-4'>
235+
<UserProfileAddSmsDialog
236+
open={open}
237+
onOpenChange={next => {
238+
if (isPending || isResending) {
239+
return;
240+
}
241+
setOpen(next);
242+
setStep('select');
243+
setDirection(1);
244+
setSelectedPhoneId('personal');
245+
setPhoneNumber('');
246+
setCode('');
247+
setErrorMessage(undefined);
248+
setResendSeconds(0);
249+
if (next) {
250+
setEnabledPhone('');
251+
hasFailed.current = false;
252+
}
253+
}}
254+
trigger={
255+
<Button
256+
variant='outline'
257+
color='neutral'
258+
>
259+
Set up SMS verification
260+
</Button>
261+
}
262+
step={step}
263+
direction={direction}
264+
phoneNumbers={phoneNumbers}
265+
selectedPhoneId={selectedPhoneId}
266+
onSelectedPhoneIdChange={id => {
267+
setSelectedPhoneId(id);
268+
setErrorMessage(undefined);
269+
}}
270+
onAddPhone={() => {
271+
setPhoneNumber('');
272+
setErrorMessage(undefined);
273+
setDirection(1);
274+
setStep('phone');
275+
}}
276+
onBack={() => {
277+
setStep(step === 'verify' ? verifyFrom : 'select');
278+
setDirection(-1);
279+
setCode('');
280+
setErrorMessage(undefined);
281+
setResendSeconds(0);
282+
}}
283+
phoneNumber={phoneNumber}
284+
onPhoneNumberChange={value => {
285+
setPhoneNumber(value);
286+
setErrorMessage(undefined);
287+
}}
288+
code={code}
289+
onCodeChange={value => {
290+
setCode(value);
291+
setErrorMessage(undefined);
292+
}}
293+
onSubmit={() => void submit()}
294+
onResend={() => void resend()}
295+
isPending={isPending}
296+
isResending={isResending}
297+
resendSeconds={resendSeconds}
298+
errorMessage={errorMessage}
299+
/>
300+
{enabledPhone ? <Text role='status'>SMS verification enabled for {enabledPhone} in this demo</Text> : null}
301+
</div>
302+
);
303+
}
304+
154305
export function Removal() {
155306
const [methods, setMethods] = useState<UserProfileMfaMethod[]>([
156307
{ id: 'authenticator', type: 'authenticator' },

0 commit comments

Comments
 (0)