import { useEffect, useState } from "react"; import { useNavigate, Link } from "react-router-dom"; import { supabase } from "@/integrations/supabase/client"; import { useAuth } from "@/hooks/useAuth"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Card } from "@/components/ui/card"; import { useToast } from "@/hooks/use-toast"; import { Sparkles, Loader2 } from "lucide-react"; export default function Auth() { const { user, loading } = useAuth(); const navigate = useNavigate(); const { toast } = useToast(); const [mode, setMode] = useState<"signin" | "signup">("signin"); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [displayName, setDisplayName] = useState(""); const [submitting, setSubmitting] = useState(false); useEffect(() => { if (!loading && user) navigate("/app", { replace: true }); }, [user, loading, navigate]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setSubmitting(true); try { if (mode === "signup") { const { error } = await supabase.auth.signUp({ email, password, options: { emailRedirectTo: `${window.location.origin}/app`, data: { display_name: displayName || email.split("@")[0] }, }, }); if (error) throw error; toast({ title: "Account created", description: "You're signed in." }); } else { const { error } = await supabase.auth.signInWithPassword({ email, password }); if (error) throw error; } } catch (err: any) { toast({ title: "Authentication failed", description: err.message ?? "Something went wrong.", variant: "destructive", }); } finally { setSubmitting(false); } }; return (

Source.AI

{mode === "signin" ? "Welcome back" : "Create your account"}

{mode === "signin" ? "Sign in to continue to your workspace." : "Start turning content into study assets."}

{mode === "signup" && (
setDisplayName(e.target.value)} placeholder="Your name" />
)}
setEmail(e.target.value)} required placeholder="you@example.com" />
setPassword(e.target.value)} required minLength={6} placeholder="••••••••" />
{mode === "signin" ? ( <> No account?{" "} ) : ( <> Already have one?{" "} )}

← Back to home

); }