import React, { useState, useEffect, useRef } from 'react'; import { UserRole } from '../types'; import { supabase } from '../supabase'; const DEPARTMENTS = ['AIDS', 'CSBS', 'CSE', 'CCE', 'MECH', 'VLSI', 'BIO-TECH', 'AIML', 'ECE', 'H&S']; const EXTERNAL_DEPARTMENTS = [ ...DEPARTMENTS, 'Information Technology (IT)', 'Electrical & Electronics Engineering (EEE)', 'Civil Engineering', 'Biomedical Engineering', 'Chemical Engineering', 'Aeronautical / Aerospace Engineering', 'Mechatronics Engineering', 'Others' ]; const SECTIONS = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']; const YEARS = ['1st Year', '2nd Year', '3rd Year', '4th Year', '5th Year']; interface LoginFormProps { role: UserRole; onSuccess: () => void; onBack: () => void; } const GoogleIcon = () => ( ); const LoginForm: React.FC = ({ role, onSuccess, onBack }) => { const [isSignUp, setIsSignUp] = useState(false); const [signUpType, setSignUpType] = useState<'INTERNAL' | 'EXTERNAL' | null>(null); const [coordinatorLoginType, setCoordinatorLoginType] = useState<'Faculty' | 'HOD' | null>(null); const [formData, setFormData] = useState({ email: '', phone: '', regNo: '', password: '', confirmPassword: '', name: '', collegeName: 'Rajalakshmi Institute of Technology', department: '', section: '', year: '', gender: '', collegeLocation: '', captchaInput: '', }); // Auto-set internal for non-student roles useEffect(() => { if (role !== 'STUDENT') { setSignUpType('INTERNAL'); } else if (!isSignUp) { setSignUpType(null); } }, [role, isSignUp]); const [showPassword, setShowPassword] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false); const [errorMessage, setErrorMessage] = useState(null); const handleGoogleSignIn = async () => { setErrorMessage(null); setIsSubmitting(true); try { const { data, error } = await supabase.auth.signInWithOAuth({ provider: 'google' }); if (error) throw error; if (data?.user) { onSuccess(); } } catch (err: any) { console.error("Google Auth error:", err); if (err.code !== 'auth/popup-closed-by-user') { setErrorMessage(err.message || "Google Sign-In failed."); } } finally { setIsSubmitting(false); } }; const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value })); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setErrorMessage(null); setIsSubmitting(true); try { if (!isSignUp) { // Login Logic const { data: loginData, error } = await supabase.auth.signInWithPassword({ email: formData.email, password: formData.password }); if (error) throw error; if (role === 'COORDINATOR' && coordinatorLoginType && loginData.user) { const { data: faculty } = await supabase.from('Facultyusers').select('role').eq('id', loginData.user.id).single(); if (!faculty || (faculty.role !== coordinatorLoginType && faculty.role !== 'System Admin')) { await supabase.auth.signOut(); throw new Error(`Access Denied: You are not provisioned as ${coordinatorLoginType}.`); } } // For students, ensure they have a profile in Studentusers if (role === 'STUDENT' && signUpType === 'INTERNAL' && loginData.user) { const { data: existing } = await supabase.from('Studentusers').select('id').eq('id', loginData.user.id).single(); if (!existing) { const { error: profileError } = await supabase.from('Studentusers').insert({ id: loginData.user.id, name: formData.name, email: formData.email, reg_no: formData.regNo, phone: formData.phone, department: formData.department, year: formData.year, section: formData.section, college_name: 'RAJALAKSHMI INSTITUTE OF TECHNOLOGY' }); if (profileError) console.error("Profile creation error:", profileError); } } onSuccess(); } else { // Sign-Up Logic const { data: signUpData, error } = await supabase.auth.signUp({ email: formData.email, password: formData.password, options: { data: { name: formData.name, role: signUpType === 'EXTERNAL' ? 'EXTERNAL_STUDENT' : role, regNo: formData.regNo, phone: formData.phone, gender: formData.gender, college: formData.collegeName, collegeLocation: formData.collegeLocation, department: formData.department, year: formData.year, section: formData.section } } }); if (error) throw error; if (signUpData.user) { if (signUpType === 'EXTERNAL') { await supabase.from('externalusers').insert({ id: signUpData.user.id, name: formData.name, email: formData.email, reg_no: formData.regNo, phone: formData.phone, department: formData.department, year: formData.year, section: formData.section, college: formData.collegeName, college_location: formData.collegeLocation, gender: formData.gender }); } else if (role === 'STUDENT') { await supabase.from('Studentusers').insert({ id: signUpData.user.id, name: formData.name, email: formData.email, reg_no: formData.regNo, phone: formData.phone, department: formData.department, year: formData.year, section: formData.section, college_name: 'RAJALAKSHMI INSTITUTE OF TECHNOLOGY' }); } } alert("Verification link sent to your email. Please verify to continue."); setIsSignUp(false); if (role === 'STUDENT') setSignUpType(null); } } catch (err: any) { setErrorMessage(err.message || "An error occurred."); } finally { setIsSubmitting(false); } }; const RIT_BLUE = 'bg-[#004a99]'; const RIT_BLUE_TEXT = 'text-[#004a99]'; const RIT_BLUE_HOVER = 'hover:bg-[#003366]'; const RIT_BLUE_BORDER = 'border-[#004a99]'; return (
{/* Main Container */}
{/* Back Button */} {/* Forms Container */}
{/* Sign Up Form (Left Side) */}
RIT Logo {!signUpType && role === 'STUDENT' ? (

Choose Portal

) : (

{role === 'STUDENT' ? (signUpType === 'INTERNAL' ? 'Internal' : 'External') : (role === 'COORDINATOR' ? 'Event Coordinator' : 'Admin')} Sign Up

{role === 'STUDENT' && }

Join the RIT Excellence Hub

{(role === 'STUDENT' || signUpType === 'EXTERNAL') && ( <>
)} {errorMessage &&

{errorMessage}

}
)}
{/* Sign In Form (Right Side) */}
{role === 'COORDINATOR' && !coordinatorLoginType ? (
RIT Logo

Choose Access Level

) : (
RIT Logo

Welcome Back

{role === 'COORDINATOR' && ( )}

Access your {role === 'ADMIN' ? 'Admin' : (role === 'COORDINATOR' ? `${coordinatorLoginType} Event` : 'Student')} portal

{errorMessage &&

{errorMessage}

} {/* Separator */}
or use Google
{/* Google Sign In Button */} )}
{/* Sliding Overlay Panel */}
{/* Background Pattern */}

{role === 'STUDENT' ? 'New Here?' : 'Restricted Access'}

{role === 'STUDENT' ? 'Sign up and discover a world of possibilities at RIT Events Hub.' : 'Sign up is restricted for this portal. Please contact the administrator to provision an account.'}

{role === 'STUDENT' && ( )}

Welcome Back!

To keep connected with us please login with your personal info.

); }; export default LoginForm;