import React from 'react';
import { Navigate, Outlet } from 'react-router-dom';
import useAuthStore from '../../store/authStore';
/**
* AdminProtectedRoute Component
* Restricts access to routes to only users with the 'admin' role.
*/
const AdminProtectedRoute = () => {
const { user, profile, loading } = useAuthStore();
if (loading) {
return (
);
}
// Check if the user is authenticated from Supabase
if (!user) {
return ;
}
// If we have a user but no profile yet, wait for the database fetch
if (!profile) {
return (
);
}
// Check if the user's profile role is 'admin' or 'super_admin'
// Enforce role
if (profile.role !== "admin" && profile.role !== "super_admin") {
// Basic redirect for non-admins if they try to access admin routes
return ;
}
// Enforce active status for admins (Master Admin approval)
if (profile.status === "rejected") {
return ;
} else if (profile.status !== "active") {
return ;
}
// Authorised and active: render the protected layout
return ;
};
export default AdminProtectedRoute;