|
1 | 1 | import { Button } from '@clerk/ui/mosaic/components/button'; |
2 | 2 | import { Text } from '@clerk/ui/mosaic/components/text'; |
3 | 3 | 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'; |
4 | 6 | import type { UserProfileMfaMethod } from '@clerk/ui/mosaic/features/user-profile/user-profile-mfa-section.view'; |
5 | 7 | 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'; |
7 | 9 |
|
8 | 10 | import type { StoryMeta } from '@/lib/types'; |
9 | 11 |
|
@@ -151,6 +153,155 @@ export function AuthenticatorSetup() { |
151 | 153 | ); |
152 | 154 | } |
153 | 155 |
|
| 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 | + |
154 | 305 | export function Removal() { |
155 | 306 | const [methods, setMethods] = useState<UserProfileMfaMethod[]>([ |
156 | 307 | { id: 'authenticator', type: 'authenticator' }, |
|
0 commit comments