import React from "react"; import { useAuth } from "~/contexts/auth-context"; import { useLocation } from "react-router"; import { LoadingPage } from "~/components/ui/loading"; interface ProtectedRouteProps { children: React.ReactNode; fallback?: React.ReactNode; requireAuth?: boolean; redirectTo?: string; } export function ProtectedRoute({ children, fallback, requireAuth = true, redirectTo = "/login", }: ProtectedRouteProps) { const { isAuthenticated, isLoading, token, user } = useAuth(); const location = useLocation(); // Show loading while checking authentication if (isLoading) { return ( fallback || (

در حال بررسی احراز هویت...

لطفاً منتظر بمانید

) ); } // If access is not allowed, render fallback and let the global route guard handle navigation/toasts if ( (requireAuth && !isAuthenticated) || (requireAuth && isAuthenticated && (!token || !token.accessToken)) || (!requireAuth && isAuthenticated && location.pathname === "/login") ) { return ( fallback || (

در حال انتقال...

لطفاً منتظر بمانید

) ); } // If all checks pass, render the protected content return <>{children}; } // Helper component for public routes export function PublicRoute({ children }: { children: React.ReactNode }) { return {children}; }