import { supabase } from '../supabaseClient'; import React, { useState } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; // --- SVG Icon Components --- const EyeIcon = () => (); const EyeOffIcon = () => (); export default function AppliLogin({ onNavigate }) { const [mode, setMode] = useState('login'); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [isPasswordVisible, setIsPasswordVisible] = useState(false); const [errors, setErrors] = useState({}); const [loading, setLoading] = useState(false); // Notification State const [notification, setNotification] = useState(null); const validate = () => { const newErrors = {}; if (!email.trim()) newErrors.email = 'Email is required.'; else if (!/\S+@\S+\.\S+/.test(email)) newErrors.email = 'Email is invalid.'; if (mode !== 'forgot') { if (!password) newErrors.password = 'Password is required.'; } if (mode === 'register') { if (password !== confirmPassword) newErrors.confirmPassword = 'Passwords do not match.'; } return newErrors; }; const handleSubmit = async (e) => { e.preventDefault(); const formErrors = validate(); if (Object.keys(formErrors).length > 0) { setErrors(formErrors); return; } setErrors({}); setLoading(true); setNotification(null); try { if (mode === 'login') { const { data, error } = await supabase.auth.signInWithPassword({ email, password, }); if (error) throw error; // Check role const role = data.user?.user_metadata?.role; if (role === 'applicant') { onNavigate('dashboard'); } else { setNotification({ type: 'error', message: 'Unauthorized role detected.' }); await supabase.auth.signOut(); } } else if (mode === 'register') { const { error } = await supabase.auth.signUp({ email, password, options: { data: { role: 'applicant' } } }); if (error) throw error; setNotification({ type: 'success', message: 'Registration successful! Please confirm your Email and login.' }); setMode('login'); } else if (mode === 'forgot') { const { error } = await supabase.auth.resetPasswordForEmail(email, { redirectTo: `${window.location.origin}/reset-password`, }); if (error) throw error; setNotification({ type: 'success', message: 'Password reset link sent! Check your email.' }); setMode('login'); } } catch (error) { setNotification({ type: 'error', message: error.message }); } finally { setLoading(false); } }; const formVariants = { hidden: { opacity: 0, y: 20 }, visible: { opacity: 1, y: 0 }, exit: { opacity: 0, y: -20 } }; const notificationStyles = { padding: '0.75rem 1rem', marginBottom: '1rem', borderRadius: '0.5rem', fontSize: '0.875rem', textAlign: 'center', border: '1px solid', }; return (
Don't have an account?
Already have an account?