import { useState, useEffect, useRef } from "react" import { createFileRoute } from "@tanstack/react-router" import { Mail } from "lucide-react" import { authClient } from "@/lib/auth-client" export const Route = createFileRoute("/auth")({ component: AuthPage, ssr: false, }) type Step = "email" | "otp" function AuthPage() { const [step, setStep] = useState("email") const emailInputRef = useRef(null) const otpInputRef = useRef(null) useEffect(() => { if (step === "email") { emailInputRef.current?.focus() } else { otpInputRef.current?.focus() } }, [step]) const [email, setEmail] = useState("") const [otp, setOtp] = useState("") const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState("") const handleSendOTP = async (e: React.FormEvent) => { e.preventDefault() if (!email.trim()) return setIsLoading(true) setError("") console.log("[auth-page] Sending OTP to:", email) try { const result = await authClient.emailOtp.sendVerificationOtp({ email, type: "sign-in", }) console.log("[auth-page] OTP result:", result) if (result.error) { console.error("[auth-page] OTP error:", result.error) setError(result.error.message || "Failed to send code") } else { console.log("[auth-page] OTP sent successfully, moving to OTP step") setStep("otp") } } catch (err) { console.error("[auth-page] Send OTP exception:", err) setError(err instanceof Error ? err.message : "Failed to send verification code") } finally { setIsLoading(false) } } const handleVerifyOTP = async (e: React.FormEvent) => { e.preventDefault() if (!otp.trim()) return setIsLoading(true) setError("") console.log("[auth-page] Verifying OTP for:", email) try { const result = await authClient.signIn.emailOtp({ email, otp, }) console.log("[auth-page] Verify result:", result) if (result.error) { console.error("[auth-page] Verify error:", result.error) setError(result.error.message || "Invalid code") } else { console.log("[auth-page] Sign in successful, redirecting...") window.location.href = "/" } } catch (err) { console.error("[auth-page] Verify OTP exception:", err) setError(err instanceof Error ? err.message : "Failed to verify code") } finally { setIsLoading(false) } } const handleResend = async () => { setIsLoading(true) setError("") setOtp("") console.log("[auth-page] Resending OTP to:", email) try { const result = await authClient.emailOtp.sendVerificationOtp({ email, type: "sign-in", }) console.log("[auth-page] Resend result:", result) if (result.error) { setError(result.error.message || "Failed to resend code") } } catch (err) { console.error("[auth-page] Resend exception:", err) setError("Failed to resend code") } finally { setIsLoading(false) } } const handleBack = () => { setStep("email") setOtp("") setError("") } return (

{step === "email" ? "Any Generation. Instantly." : "Enter your code"}

{step === "email" ? "Text, images/video on canvas. Fancy context management. Just think it and it's there." : `We sent a 6-digit code to ${email}`}

{step === "email" ? (

Enter your email and we'll send you a verification code.

{error && (

{error}

)}
) : (
{error && (

{error}

)}
)}
) }