import React, { useState, useContext } from 'react'; import { X, Mail, Lock, Loader2, Shield, Eye, EyeOff } from 'lucide-react'; import { PlayerContext } from '../PlayerContext'; import { GoogleLogin } from '@react-oauth/google'; export default function LoginModal({ isOpen, onClose }) { const { setIsLoggedIn, setUserProfile, setHasGuestMadeEdits } = useContext(PlayerContext); const [isSignUp, setIsSignUp] = useState(false); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const [showConfirmPassword, setShowConfirmPassword] = useState(false); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); const [isSuccess, setIsSuccess] = useState(false); if (!isOpen) return null; const toggleMode = () => { setIsSignUp(!isSignUp); setError(''); setPassword(''); setConfirmPassword(''); }; const handleSubmit = async (e) => { e.preventDefault(); setIsLoading(true); setError(''); // Reconfirmation Validation for Sign Up if (isSignUp && password !== confirmPassword) { setError('Passwords do not match!'); setIsLoading(false); return; } const endpoint = isSignUp ? '/api/auth/register' : '/api/auth/login'; try { const res = await fetch(`https://anayshukla-fpl-solver.hf.space${endpoint}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }) }); const data = await res.json(); if (!res.ok) { throw new Error(data.detail || 'Authentication failed'); } // Success! Save token and update Global Context localStorage.setItem('fpl_token', data.access_token); setUserProfile({ username: data.email.split('@')[0], defaultTeamId: null, isAdmin: data.is_admin }); setIsLoggedIn(true); setHasGuestMadeEdits(false); setIsSuccess(true); setTimeout(() => onClose(), 100); } catch (err) { setError(err.message); } finally { setIsLoading(false); } }; return (
{/* Header */}

{isSignUp ? 'Create Account' : 'Welcome Back'}

{/* Body */}
{error && (
{error}
)}
{/* Email Field */}
setEmail(e.target.value)} className="w-full bg-slate-900 border border-slate-700 rounded-xl py-3 pl-10 pr-4 text-sm text-slate-200 focus:outline-none focus:border-luigi-400 transition-colors" />
{/* Password Field */}
setPassword(e.target.value)} className="w-full bg-slate-900 border border-slate-700 rounded-xl py-3 pl-10 pr-10 text-sm text-slate-200 focus:outline-none focus:border-luigi-400 transition-colors" />
{/* Confirm Password Field (Only for Sign Up) */} {isSignUp && (
setConfirmPassword(e.target.value)} className={`w-full bg-slate-900 border rounded-xl py-3 pl-10 pr-10 text-sm text-slate-200 focus:outline-none transition-colors ${ confirmPassword && password !== confirmPassword ? "border-red-500/50 focus:border-red-500" : "border-slate-700 focus:border-luigi-400" }`} />
)}
{/* Toggle Login/Signup Mode */}
{isSignUp ? 'Already have an account?' : "Don't have an account?"}
{/* Elegant "OR" Divider */}
OR
{/* Google Login Block */}
{ try { const res = await fetch('https://anayshukla-fpl-solver.hf.space/api/auth/google', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ token: credentialResponse.credential }) }); const data = await res.json(); if (!res.ok) throw new Error(data.detail || "Google Auth Failed"); localStorage.setItem('fpl_token', data.access_token); setUserProfile({ username: data.email.split('@')[0], defaultTeamId: null, isAdmin: data.is_admin }); setIsLoggedIn(true); setHasGuestMadeEdits(false); setIsSuccess(true); setTimeout(() => onClose(), 100); } catch (err) { setError(err.message); } }} onError={() => { setError('Google Login window closed or failed.'); }} theme="filled_black" shape="pill" />
{isSuccess && (

Entering Mansion...

)}
); }