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 (
Entering Mansion...