fmpro / frontend /src /components /ProtectedRoute.tsx
Hkoradiya123
feat: Initialize frontend with Zustand for state management, Tailwind CSS for styling, and TypeScript for type safety
4854e2b
Raw History Blame Contribute Delete
976 Bytes
/**
* Protected Route Component
*/
import React from 'react';
import { Navigate } from 'react-router-dom';
import { useAuth } from '../hooks';
interface ProtectedRouteProps {
children: React.ReactNode;
adminOnly?: boolean;
}
export const ProtectedRoute: React.FC<ProtectedRouteProps> = ({ children, adminOnly = false }) => {
const { isAuthenticated, user, isLoading } = useAuth();
if (isLoading) {
return (
<div className="app-shell flex min-h-screen items-center justify-center">
<div className="text-center">
<div className="inline-block h-8 w-8 animate-spin rounded-full border-4 border-[color:var(--brand)] border-t-transparent" />
<p className="mt-2 text-[color:var(--muted)]">Loading...</p>
</div>
</div>
);
}
if (!isAuthenticated) {
return <Navigate to="/login" />;
}
if (adminOnly && user?.role !== 'admin') {
return <Navigate to="/dashboard" />;
}
return <>{children}</>;
};