import { Trans } from "@lingui/react/macro" import { LoaderCircle, MailIcon, SendHorizonalIcon } from "lucide-react" import { useCallback, useState } from "react" import { InputOTP, InputOTPGroup, InputOTPSlot } from "@/components/ui/otp" import { pb } from "@/lib/api" import { $authenticated } from "@/lib/stores" import { cn } from "@/lib/utils" import { $router } from "../router" import { buttonVariants } from "../ui/button" import { Input } from "../ui/input" import { Label } from "../ui/label" import { showLoginFaliedToast } from "./auth-form" export function OtpInputForm({ otpId, mfaId }: { otpId: string; mfaId: string }) { const [value, setValue] = useState("") if (value.length === 6) { pb.collection("users") .authWithOTP(otpId, value, { mfaId }) .then(() => { $router.open("/") $authenticated.set(true) }) .catch((err) => { showLoginFaliedToast(err.message) }) } return (
{Array.from({ length: 6 }).map((_, i) => ( ))}
Enter your one-time password.
) } export function OtpRequestForm() { const [isLoading, setIsLoading] = useState(false) const [email, setEmail] = useState("") const [otpId, setOtpId] = useState() const handleSubmit = useCallback( async (e: React.FormEvent) => { e.preventDefault() setIsLoading(true) try { // console.log(email) const { otpId } = await pb.collection("users").requestOTP(email) setOtpId(otpId) } catch (e: any) { showLoginFaliedToast(e?.message) } finally { setIsLoading(false) setEmail("") } }, [email] ) if (otpId) { return } return (
setEmail(e.target.value)} id="email" name="email" required placeholder="name@example.com" type="email" autoCapitalize="none" autoComplete="email" autoCorrect="off" disabled={isLoading} className="ps-9" />
) }