import React, { useState } from 'react'; import { supabase } from '../services/supabaseClient'; import { LoadingSpinner } from './LoadingSpinner'; import { XMarkIcon } from './icons/XMarkIcon'; import { GoogleIcon } from './icons/GoogleIcon'; import { GithubIcon } from './icons/GithubIcon'; interface AuthModalProps { isOpen: boolean; onClose: () => void; } type AuthView = 'signIn' | 'signUp' | 'resetPassword'; export const AuthModal: React.FC = ({ isOpen, onClose }) => { const [view, setView] = useState('signIn'); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [message, setMessage] = useState(null); const clearState = () => { setError(null); setMessage(null); setEmail(''); setPassword(''); } const handleViewChange = (newView: AuthView) => { setView(newView); clearState(); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(null); setMessage(null); try { if (view === 'signUp') { const { error } = await supabase.auth.signUp({ email, password }); if (error) throw error; setMessage('Check your email for the confirmation link!'); } else { const { error } = await supabase.auth.signInWithPassword({ email, password }); if (error) throw error; onClose(); } } catch (err: any) { setError(err.message || 'An unexpected error occurred.'); } finally { setLoading(false); } }; const handlePasswordReset = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(null); setMessage(null); try { const { error } = await supabase.auth.resetPasswordForEmail(email, { redirectTo: window.location.href, }); if (error) throw error; setMessage('Password reset link sent! Check your email.'); } catch(err: any) { setError(err.message || 'An unexpected error occurred.'); } finally { setLoading(false); } }; const handleOAuthSignIn = async (provider: 'google' | 'github') => { setLoading(true); setError(null); const { error } = await supabase.auth.signInWithOAuth({ provider, options: { redirectTo: window.location.href, } }); if (error) { setError(error.message); setLoading(false); } }; if (!isOpen) return null; return (
e.stopPropagation()} >
{view !== 'resetPassword' && (
)} {view === 'resetPassword' ? ( <>

Reset Password

Enter your email to receive a reset link.

setEmail(e.target.value)} required className="mt-1 block w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm" placeholder="you@example.com"/>
{error &&

{error}

} {message &&

{message}

}
) : ( <>

{view === 'signUp' ? 'Create an Account' : 'Welcome Back'}

{view === 'signUp' ? 'to start personalizing your experience.' : 'to access your watched items and lists.'}

OR
setEmail(e.target.value)} required className="mt-1 block w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-brand-primary focus:border-brand-primary" placeholder="you@example.com" />
{view === 'signIn' && ( )}
setPassword(e.target.value)} required className="mt-1 block w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-brand-primary focus:border-brand-primary" placeholder="••••••••" />
{error &&

{error}

} {message &&

{message}

}
)}
); };