import { useState, useEffect, useRef } from "react" import { createFileRoute } from "@tanstack/react-router" import { Mail, Apple, Github } from "lucide-react" import { authClient } from "@/lib/auth-client" export const Route = createFileRoute("/auth")({ component: AuthPage, ssr: false, }) type Step = "email" | "otp" function ChromeIcon({ className }: { className?: string }) { return ( ) } 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 ( Welcome to Linsa! {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. Email setEmail(e.target.value)} className="mt-2 w-full rounded-2xl border border-white/10 bg-white/5 px-4 py-3 text-sm text-white placeholder:text-white/40 focus:border-white/40 focus:outline-none focus:ring-0" /> {error && ( {error} )} {isLoading ? "Sending code..." : "Send verification code"} ) : ( Verification Code setOtp(e.target.value.replace(/\D/g, ""))} className="mt-2 w-full rounded-2xl border border-white/10 bg-white/5 px-4 py-3 text-center text-2xl font-mono tracking-[0.5em] text-white placeholder:text-white/40 focus:border-white/40 focus:outline-none focus:ring-0" /> {error && ( {error} )} {isLoading ? "Verifying..." : "Sign in"} ← Back Resend code )} Coming soon Apple Google GitHub ) }
{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}`}
Enter your email and we'll send you a verification code.
{error}
Coming soon