Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/components/auth/forms/email-otp-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import { useContext, useEffect, useState } from "react"
import { useForm } from "react-hook-form"
import * as z from "zod"

import type { BetterFetchOption } from "better-auth/react"
import { useCaptcha } from "../../../hooks/use-captcha"
import { useIsHydrated } from "../../../hooks/use-hydrated"
import { useOnSuccessTransition } from "../../../hooks/use-success-transition"
import { AuthUIContext } from "../../../lib/auth-ui-provider"
import { cn, getLocalizedError } from "../../../lib/utils"
import type { AuthLocalization } from "../../../localization/auth-localization"
import { Captcha } from "../../captcha/captcha"
import { Button } from "../../ui/button"
import {
Form,
Expand Down Expand Up @@ -57,6 +60,7 @@ function EmailForm({
setEmail: (email: string) => void
}) {
const isHydrated = useIsHydrated()
const { captchaRef, getCaptchaHeaders } = useCaptcha({ localization })

const {
authClient,
Expand Down Expand Up @@ -91,11 +95,16 @@ function EmailForm({
}, [form.formState.isSubmitting, setIsSubmitting])

async function sendEmailOTP({ email }: z.infer<typeof formSchema>) {
const fetchOptions: BetterFetchOption = {
throw: true,
headers: await getCaptchaHeaders("/email-otp/send-verification-otp")
}

try {
await authClient.emailOtp.sendVerificationOtp({
email,
type: "sign-in",
fetchOptions: { throw: true }
fetchOptions
})

toast({
Expand Down Expand Up @@ -143,6 +152,12 @@ function EmailForm({
)}
/>

<Captcha
ref={captchaRef}
localization={localization}
action="/email-otp/send-verification-otp"
/>

<Button
type="submit"
disabled={isSubmitting}
Expand Down