Spaces:
Running
Running
File size: 9,983 Bytes
f7cecf3 24bb13e f7cecf3 24bb13e f7cecf3 052f3f2 24bb13e f7cecf3 24bb13e f7cecf3 24bb13e 418395e 24bb13e f7cecf3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | 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 (
<div className="fixed inset-0 z-modal flex items-center justify-center bg-black/80 backdrop-blur-sm p-3 sm:p-4">
<div className="relative bg-slate-950 border border-slate-800 w-full max-w-md rounded-2xl shadow-2xl overflow-hidden animate-in zoom-in-95 duration-200 max-h-[90vh] overflow-y-auto custom-scrollbar">
{/* Header */}
<div className="bg-slate-900 p-5 flex justify-between items-center border-b border-slate-800">
<div className="flex items-center gap-3">
<div className="w-8 h-8 bg-luigi-500/20 text-luigi-400 rounded-lg flex items-center justify-center">
<Shield size={18} />
</div>
<h2 className="text-xl font-black text-slate-100">
{isSignUp ? 'Create Account' : 'Welcome Back'}
</h2>
</div>
<button onClick={onClose} className="text-slate-500 hover:text-white transition-colors bg-slate-950 p-1.5 rounded-full border border-slate-800">
<X size={18} />
</button>
</div>
{/* Body */}
<div className="p-6">
{error && (
<div className="mb-4 p-3 bg-red-950/30 border border-red-900/50 text-red-400 text-sm rounded-lg text-center font-bold">
{error}
</div>
)}
<form onSubmit={handleSubmit} className="flex flex-col gap-4">
{/* Email Field */}
<div className="relative">
<Mail className="absolute left-3 top-1/2 -translate-y-1/2 text-slate-500" size={16} />
<input
type="email"
required
placeholder="Email Address"
value={email}
onChange={(e) => 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"
/>
</div>
{/* Password Field */}
<div className="relative">
<Lock className="absolute left-3 top-1/2 -translate-y-1/2 text-slate-500" size={16} />
<input
type={showPassword ? "text" : "password"}
required
placeholder="Password"
value={password}
onChange={(e) => 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"
/>
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-500 hover:text-slate-300 transition-colors"
>
{showPassword ? <EyeOff size={16} /> : <Eye size={16} />}
</button>
</div>
{/* Confirm Password Field (Only for Sign Up) */}
{isSignUp && (
<div className="relative animate-in slide-in-from-top-2 fade-in duration-200">
<Lock className="absolute left-3 top-1/2 -translate-y-1/2 text-slate-500" size={16} />
<input
type={showConfirmPassword ? "text" : "password"}
required
placeholder="Confirm Password"
value={confirmPassword}
onChange={(e) => 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"
}`}
/>
<button
type="button"
onClick={() => setShowConfirmPassword(!showConfirmPassword)}
className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-500 hover:text-slate-300 transition-colors"
>
{showConfirmPassword ? <EyeOff size={16} /> : <Eye size={16} />}
</button>
</div>
)}
<button
type="submit"
disabled={isLoading}
className="w-full bg-luigi-500 hover:bg-luigi-400 text-slate-950 py-3 rounded-xl font-bold text-sm transition-colors shadow-lg shadow-luigi-500/20 flex justify-center items-center mt-2"
>
{isLoading ? <Loader2 size={18} className="animate-spin" /> : (isSignUp ? 'Create Account' : 'Log In')}
</button>
</form>
{/* Toggle Login/Signup Mode */}
<div className="mt-6 flex items-center justify-between text-sm text-slate-500">
<span>{isSignUp ? 'Already have an account?' : "Don't have an account?"}</span>
<button
onClick={toggleMode}
className="text-luigi-400 font-bold hover:underline"
>
{isSignUp ? 'Log In' : 'Sign Up'}
</button>
</div>
{/* Elegant "OR" Divider */}
<div className="mt-6 mb-2 relative flex items-center justify-center">
<div className="absolute inset-0 flex items-center">
<div className="w-full border-t border-slate-800"></div>
</div>
<div className="relative px-4 bg-slate-950 text-xs font-bold text-slate-500 uppercase tracking-widest">
OR
</div>
</div>
{/* Google Login Block */}
<div className="mt-4 flex justify-center">
<GoogleLogin
text={isSignUp ? "signup_with" : "signin_with"}
onSuccess={async (credentialResponse) => {
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"
/>
</div>
</div>
</div>
{isSuccess && (
<div className="fixed inset-0 bg-slate-950 flex flex-col items-center justify-center gap-3 z-critical">
<div className="w-10 h-10 border-4 border-slate-800 border-t-luigi-500 rounded-full animate-spin shadow-[0_0_15px_rgba(16,185,129,0.5)]" />
<p className="text-luigi-400 font-bold tracking-widest uppercase text-xs animate-pulse">
Entering Mansion...
</p>
</div>
)}
</div>
);
} |