|
| 1 | +import { Button } from '@/src/components/shadcn-ui/button'; |
| 2 | +import { |
| 3 | + InputOTP, |
| 4 | + InputOTPGroup, |
| 5 | + InputOTPSlot |
| 6 | +} from '@/src/components/shadcn-ui/input-otp'; |
| 7 | +import { |
| 8 | + AlertDialog, |
| 9 | + AlertDialogContent, |
| 10 | + AlertDialogTitle, |
| 11 | + AlertDialogDescription |
| 12 | +} from '@/src/components/shadcn-ui/alert-dialog'; |
| 13 | +import { useRouter } from 'next/navigation'; |
| 14 | +import { useState } from 'react'; |
| 15 | +import { platform } from '@/src/lib/trpc'; |
| 16 | + |
| 17 | +export function TwoFactorDialog({ open }: { open: boolean }) { |
| 18 | + const [code, setCode] = useState(''); |
| 19 | + const router = useRouter(); |
| 20 | + const { isSuccess, error, mutateAsync, isPending } = |
| 21 | + platform.auth.twoFactorAuthentication.verifyTwoFactorChallenge.useMutation(); |
| 22 | + |
| 23 | + return ( |
| 24 | + <AlertDialog open={open}> |
| 25 | + <AlertDialogContent className="w-min p-8"> |
| 26 | + <AlertDialogTitle>Two Factor Authentication</AlertDialogTitle> |
| 27 | + <AlertDialogDescription> |
| 28 | + Please enter the 6-digit code from your authenticator app |
| 29 | + </AlertDialogDescription> |
| 30 | + <div className="mx-auto w-fit"> |
| 31 | + <InputOTP |
| 32 | + maxLength={6} |
| 33 | + value={code} |
| 34 | + onChange={(e) => setCode(e)}> |
| 35 | + <InputOTPGroup> |
| 36 | + <InputOTPSlot index={0} /> |
| 37 | + <InputOTPSlot index={1} /> |
| 38 | + <InputOTPSlot index={2} /> |
| 39 | + <InputOTPSlot index={3} /> |
| 40 | + <InputOTPSlot index={4} /> |
| 41 | + <InputOTPSlot index={5} /> |
| 42 | + </InputOTPGroup> |
| 43 | + </InputOTP> |
| 44 | + </div> |
| 45 | + {error && ( |
| 46 | + <div className="text-destructive text-center text-sm"> |
| 47 | + {error.message} |
| 48 | + </div> |
| 49 | + )} |
| 50 | + <Button |
| 51 | + className="mx-auto w-full" |
| 52 | + variant="secondary" |
| 53 | + disabled={code.length !== 6 || isPending || isSuccess} |
| 54 | + loading={isPending || isSuccess} |
| 55 | + onClick={async () => { |
| 56 | + try { |
| 57 | + const { defaultOrgSlug } = await mutateAsync({ |
| 58 | + twoFactorCode: code |
| 59 | + }); |
| 60 | + if (!defaultOrgSlug) { |
| 61 | + router.push('/join/org'); |
| 62 | + } else { |
| 63 | + router.push(`/${defaultOrgSlug}`); |
| 64 | + } |
| 65 | + } catch { |
| 66 | + /* do nothing */ |
| 67 | + } |
| 68 | + }}> |
| 69 | + {isSuccess ? 'Redirecting...' : isPending ? 'Verifying...' : 'Verify'} |
| 70 | + </Button> |
| 71 | + </AlertDialogContent> |
| 72 | + </AlertDialog> |
| 73 | + ); |
| 74 | +} |
0 commit comments