pos-dashboard/src/components/ProtectedRoute.jsx

18 lines
553 B
React
Raw Normal View History

2025-08-02 02:33:10 +07:00
import { useSelector } from 'react-redux';
2025-08-03 11:14:55 +07:00
import { Navigate } from 'react-router-dom';
2025-08-02 02:33:10 +07:00
const ProtectedRoute = ({ children }) => {
// Check if user is authenticated using Redux state
const authState = useSelector((state) => state.auth);
const isAuthenticated = authState?.isAuthenticated || authState?.token;
2025-08-03 11:14:55 +07:00
if (!isAuthenticated) {
2025-08-02 02:33:10 +07:00
// Redirect to login page if not authenticated
return <Navigate to="/signin" replace />;
}
// If authenticated, render the protected component
return children;
};
export default ProtectedRoute;