All files / src/components AdminRoute.tsx

73.07% Statements 19/26
50% Branches 2/4
100% Functions 1/1
73.07% Lines 19/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 341x 1x 1x 1x   2x 2x 2x 2x   2x               2x       2x 1x 1x 1x 1x 1x   1x   1x 1x  
import { Navigate, useLocation } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { useAuth } from '@/contexts/AuthContext';
import AccessDeniedView from '@/views/fallback/AccessDeniedView';
 
export default function AdminRoute({ children }: { children: React.ReactNode }) {
  const { t } = useTranslation();
  const { token, role, isLoading } = useAuth();
  const location = useLocation();
 
  if (isLoading) {
    return (
      <div className="auth-page">
        <div className="spinner spinner-themed" />
      </div>
    );
  }
 
  if (!token) {
    return <Navigate to="/" state={{ from: location.pathname }} replace />;
  }
 
  if (role !== 'ADMIN') {
    return (
      <AccessDeniedView
        title={t('fallback.access_denied.admin_title')}
        message={t('fallback.access_denied.admin_body')}
      />
    );
  }
 
  return <>{children}</>;
}