import { supabase } from '../supabaseClient';
import React, { useState } from 'react';
import { motion } from 'framer-motion';
// --- SVG Icon Components ---
const UserIcon = () => ();
const LockIcon = () => ();
const SpinnerIcon = () => ;
export default function AdminLogin({ onNavigate }) {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const handleAdminLogin = async (e) => {
e.preventDefault();
setError('');
if (!email.trim() || !password) {
setError('Email and password are required.');
return;
}
setLoading(true);
try {
// 1. Authenticate
const { data: { user }, error: authError } = await supabase.auth.signInWithPassword({
email: email,
password: password,
});
if (authError) throw authError;
if (!user) throw new Error("Login failed.");
// 2. Check Role
const { data: roleData, error: roleError } = await supabase
.from('user_roles')
.select('role')
.eq('user_id', user.id)
.single();
if (roleError && roleError.code !== 'PGRST116') {
throw new Error("Could not verify user role.");
}
// 3. Admin Check
// Accepts 'admin' or 'recruiter' based on your previous logic
if (roleData && (roleData.role === 'admin' || roleData.role === 'recruiter')) {
if (typeof onNavigate === 'function') {
onNavigate('admin-dashboard');
}
} else {
await supabase.auth.signOut();
throw new Error("Access Denied. Authorized personnel only.");
}
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
};
return (
{/* --- 🔴 FIXED BACK BUTTON --- */}
{/* Background Shapes */}
<>
>
Admin Portal
Manage CV submissions and applications
);
}