MCPcopy Create free account
hub / github.com/better-auth/examples / TwoFactorComponent

Function TwoFactorComponent

astro-example/src/components/two-factor.tsx:18–88  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16import { twoFactorActions } from "@/libs/auth-client";
17
18export function TwoFactorComponent() {
19 const [otp, setOTP] = createSignal("");
20
21 createEffect(() => {
22 if (otp().length === 6) {
23 twoFactorActions.verifyTotp({
24 code: otp(),
25 fetchOptions: {
26 onError(context) {
27 if (context.error.status === 429) {
28 const retryAfter = context.response.headers.get("X-Retry-After");
29 alert(
30 `Too many requests. Please try again after ${retryAfter} seconds`,
31 );
32 } else {
33 alert(
34 context.error.message ||
35 context.error.statusText ||
36 context.error.status,
37 );
38 }
39 },
40 },
41 });
42 }
43 });
44 return (
45 <main class="flex flex-col items-center justify-center min-h-[calc(100vh-10rem)]">
46 <Card class="w-[350px]">
47 <CardHeader>
48 <CardTitle>TOTP Verification</CardTitle>
49 <CardDescription>
50 Enter your 6-digit TOTP code to authenticate
51 </CardDescription>
52 </CardHeader>
53 <CardContent>
54 <div class="flex flex-col gap-2 items-center">
55 <OTPField
56 maxLength={6}
57 value={otp()}
58 onValueChange={(value) => {
59 setOTP(value);
60 }}
61 >
62 <OTPFieldInput />
63 <OTPFieldGroup>
64 <OTPFieldSlot index={0} />
65 <OTPFieldSlot index={1} />
66 <OTPFieldSlot index={2} />
67 <OTPFieldSlot index={3} />
68 <OTPFieldSlot index={4} />
69 <OTPFieldSlot index={5} />
70 </OTPFieldGroup>
71 </OTPField>
72 <span class="text-center text-xs">
73 Enter your one-time password.
74 </span>
75 </div>

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected