| 1 | "use client"; |
| 2 | |
| 3 | import type * as React |
| 4 | import { useState } |
| 5 | import { useRouter } |
| 6 | import { useTranslations } |
| 7 | |
| 8 | import { Button } |
| 9 | import { Input } |
| 10 | import { Label } |
| 11 | |
| 12 | import AuthShell from "@/components/auth-shell |
| 13 | import { AuthSettingsProvider, useAuthSettings |
| 14 | import { authClient } |
| 15 | |
| 16 | export default function TwoFactorPage |
| 17 | const { appName, |
| 18 | return ( |
| 19 | <AuthSettingsProvider value={ |
| 20 | <TwoFactorForm /> |
| 21 | </AuthSettingsProvider> |
| 22 | ); |
| 23 | } |
| 24 | |
| 25 | function TwoFactorForm() { |
| 26 | const t = |
| 27 | const router = |
| 28 | const [code, |
| 29 | const [isBackupMode, |
| 30 | const [error, |
| 31 | const [pending, |
| 32 | |
| 33 | async function handleVerify |
| 34 | if (!codeValue.trim |
| 35 | setError(""); |
| 36 | setPending(true); |
| 37 | try { |
| 38 | if (isBackupMode) { |
| 39 | const res = |
| 40 | if (res.error |
| 41 | } else { |
| 42 | const res = |
| 43 | if (res.error |
| 44 | } |
| 45 | router.push("/ |
| 46 | } catch (e) |
| 47 | setError(e instanceof |
| 48 | setPending(false); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | function handleChange(e |
| 53 | const v = |
| 54 | setCode(v); |
| 55 | if (!isBackupMode && |
| 56 | } |
| 57 | |
| 58 | function switchMode() { |
| 59 | setIsBackupMode((b) |
| 60 | setCode(""); |
| 61 | setError(""); |
| 62 | } |
| 63 | |
| 64 | const isVerifyDisabled = |
| 65 | |
| 66 | return ( |
| 67 | <AuthShell |
| 68 | title={t(" |
| 69 | subtitle={isBackupMode ? |
| 70 | > |
| 71 | <div className="flex flex-col gap-3.5 |
| 72 | <div className="flex flex-col gap-2 |
| 73 | <Label htmlFor=" |
| 74 | {isBackupMode ? t |
| 75 | </Label> |
| 76 | <Input |
| 77 | id="2fa-code" |
| 78 | autoFocus |
| 79 | autoComplete="one-time-code" |
| 80 | inputMode={isBackupMode ? |
| 81 | className="text-center font-mono tracking-widest" |
| 82 | placeholder={isBackupMode ? |
| 83 | value={code} |
| 84 | onChange={handleChange} |
| 85 | onKeyDown={(e) |
| 86 | disabled={pending} |
| 87 | aria-invalid={error ? |
| 88 | /> |
| 89 | {error ? ( |
| 90 | <p className="flex items-center gap-1 text-xs text-destructive |
| 91 | <svg className="h-3 w-3 shrink-0 |
| 92 | <path d="M8 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1zm-.75 3.75a.75.75 0 0 1 1.5 0v3.5a.75.75 0 0 1-1.5 0v-3.5zm.75 6.5a.875.875 0 1 1 0-1.75.875.875 0 0 1 0 1.75z |
| 93 | </svg> |
| 94 | {error} |
| 95 | </p> |
| 96 | ) : null} |
| 97 | </div> |
| 98 | |
| 99 | <Button type=" |
| 100 | {pending ? t |
| 101 | </Button> |
| 102 | |
| 103 | <button |
| 104 | type="button" |
| 105 | onClick={switchMode} |
| 106 | className="text-center text-xs text-muted-foreground underline-offset-2 hover:text-foreground hover:underline" |
| 107 | > |
| 108 | {isBackupMode ? t |
| 109 | </button> |
| 110 | </div> |
| 111 | </AuthShell> |
| 112 | ); |
| 113 | } |