import { useState } from 'react'; import { useSearchParams, useNavigate } from 'react-router-dom'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { supabase } from '@/lib/supabase'; import { useToast } from '@/hooks/use-toast'; import { Loader2, ArrowLeft } from 'lucide-react'; export default function PasswordReset() { const [searchParams] = useSearchParams(); const navigate = useNavigate(); const { toast } = useToast(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [loading, setLoading] = useState(false); const token = searchParams.get('token'); const handleRequestReset = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { const { data, error } = await supabase.functions.invoke('password-reset', { body: { action: 'request', email } }); if (error) throw error; toast({ title: "Reset Email Sent", description: "If the email exists, you'll receive a password reset link", }); setEmail(''); } catch (error: any) { toast({ title: "Error", description: error.message || "Failed to send reset email", variant: "destructive" }); } finally { setLoading(false); } }; const handleResetPassword = async (e: React.FormEvent) => { e.preventDefault(); if (password !== confirmPassword) { toast({ title: "Error", description: "Passwords do not match", variant: "destructive" }); return; } if (password.length < 8) { toast({ title: "Error", description: "Password must be at least 8 characters", variant: "destructive" }); return; } setLoading(true); try { const { data, error } = await supabase.functions.invoke('password-reset', { body: { action: 'reset', token, newPassword: password } }); if (error) throw error; toast({ title: "Password Reset Successful", description: "Your password has been updated", }); navigate('/login'); } catch (error: any) { toast({ title: "Error", description: error.message || "Failed to reset password", variant: "destructive" }); } finally { setLoading(false); } }; return (