import { useState, useEffect, FormEvent } from 'react'; import { Link } from 'react-router-dom'; import { authApi } from '../api/client'; interface AuthFormProps { mode: 'login' | 'register'; onSubmit: (email: string, password: string) => Promise; } export default function AuthForm({ mode, onSubmit }: AuthFormProps) { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [error, setError] = useState(''); const [isLoading, setIsLoading] = useState(false); const [registrationEnabled, setRegistrationEnabled] = useState(null); const [theme, setTheme] = useState<'light' | 'dark'>(() => { const saved = localStorage.getItem('theme'); return (saved as 'light' | 'dark') || 'light'; }); useEffect(() => { document.documentElement.setAttribute('data-theme', theme); localStorage.setItem('theme', theme); }, [theme]); useEffect(() => { // Check if registration is enabled authApi.getRegistrationStatus() .then(res => setRegistrationEnabled(res.data.registration_enabled)) .catch(() => setRegistrationEnabled(true)); // Default to true on error }, []); const toggleTheme = () => { setTheme((prev) => (prev === 'light' ? 'dark' : 'light')); }; const handleSubmit = async (e: FormEvent) => { e.preventDefault(); setError(''); if (mode === 'register' && password !== confirmPassword) { setError('Passwords do not match'); return; } if (password.length < 8) { setError('Password must be at least 8 characters'); return; } setIsLoading(true); try { await onSubmit(email, password); } catch (err) { if (err instanceof Error) { // eslint-disable-next-line @typescript-eslint/no-explicit-any const axiosError = err as any; setError(axiosError.response?.data?.error || 'An error occurred'); } else { setError('An error occurred'); } } finally { setIsLoading(false); } }; return (
👻

PriceGhost

{mode === 'login' ? 'Sign in to track prices' : 'Create your account'}

{error &&
{error}
}
setEmail(e.target.value)} placeholder="you@example.com" required autoComplete="email" />
setPassword(e.target.value)} placeholder="••••••••" required autoComplete={mode === 'login' ? 'current-password' : 'new-password'} />
{mode === 'register' && (
setConfirmPassword(e.target.value)} placeholder="••••••••" required autoComplete="new-password" />
)}
{mode === 'login' && registrationEnabled && (
Don't have an account? Sign up
)} {mode === 'register' && (
Already have an account? Sign in
)}
); }