import { useEffect, useRef, useState } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { X, ShieldCheck, Sparkles, User, KeyRound, ArrowRight, AlertCircle, GraduationCap, Bus, Shield } from 'lucide-react'; import { useAuth } from '@/context/AuthContext'; import { useNavigate } from 'react-router-dom'; interface GoogleAuthModalProps { isOpen: boolean; onClose: () => void; } export default function GoogleAuthModal({ isOpen, onClose }: GoogleAuthModalProps) { const buttonRef = useRef(null); const navigate = useNavigate(); const { isAuthenticated, loginWithCredentials } = useAuth(); const [usernameInput, setUsernameInput] = useState(''); const [passwordInput, setPasswordInput] = useState(''); const [isLoading, setIsLoading] = useState(false); const [errorMessage, setErrorMessage] = useState(null); const [activeMode, setActiveMode] = useState<'credentials' | 'google'>('credentials'); // Close modal automatically if user gets authenticated useEffect(() => { if (isAuthenticated && isOpen) { onClose(); } }, [isAuthenticated, isOpen, onClose]); // Render Google Button inside modal when open useEffect(() => { if (!isOpen || activeMode !== 'google') return; const renderGoogleBtn = () => { if (window.google && buttonRef.current) { buttonRef.current.innerHTML = ''; window.google.accounts.id.renderButton(buttonRef.current, { type: 'standard', theme: 'outline', size: 'large', text: 'signin_with', shape: 'pill', logo_alignment: 'left', width: 280, }); } }; renderGoogleBtn(); const interval = setInterval(() => { if (window.google) { renderGoogleBtn(); clearInterval(interval); } }, 200); return () => clearInterval(interval); }, [isOpen, activeMode]); const handleSubmitCredentials = async (e: React.FormEvent) => { e.preventDefault(); if (!usernameInput.trim() || !passwordInput.trim()) { setErrorMessage('Please enter both username/register number and password.'); return; } setIsLoading(true); setErrorMessage(null); try { const result = await loginWithCredentials(usernameInput.trim(), passwordInput.trim()); if (result.success) { onClose(); if (result.redirectTo) { navigate(result.redirectTo); } } else { setErrorMessage(result.message || 'Invalid credentials.'); } } catch (err: any) { setErrorMessage(err?.message || 'Authentication error.'); } finally { setIsLoading(false); } }; const handleQuickFill = (user: string, pass: string) => { setUsernameInput(user); setPasswordInput(pass); setErrorMessage(null); }; return ( {isOpen && (
{/* Backdrop */} {/* Modal Container */} {/* Background Glow */}
{/* Close Button */} {/* Header */}

Sign In to Freshers Hub

Enter your student register number or admin credentials.

{/* Tab Selector */}
{/* ERROR MESSAGE */} {errorMessage && (
{errorMessage}
)} {/* 1. CREDENTIALS SIGN IN FORM */} {activeMode === 'credentials' && (
setUsernameInput(e.target.value)} placeholder="e.g. 2114251001 or Transport or Admin" className="w-full bg-slate-950 border border-white/10 rounded-xl px-4 py-2.5 text-xs text-white placeholder-slate-500 outline-none focus:border-orange-500 transition-colors font-mono" required />
setPasswordInput(e.target.value)} placeholder="••••••••" className="w-full bg-slate-950 border border-white/10 rounded-xl px-4 py-2.5 text-xs text-white placeholder-slate-500 outline-none focus:border-orange-500 transition-colors font-mono" required />
{/* Quick Demo Fill Buttons */}
Quick Demo Credentials:
)} {/* 2. GOOGLE SIGN IN */} {activeMode === 'google' && (

Sign in with your Google account. Use your{' '} @ritchennai.edu.in email for automatic student verification.

Google OAuth 2.0 Single Sign-On
)}
)} ); }