RequireAuth.tsx
470 Bytes
import { Navigate, useLocation } from 'react-router-dom';
import { useAppSelector } from '../store/hooks';
import { selectIsAuthenticated } from '../store/slices/authSlice';
export default function RequireAuth({ children }: { children: React.ReactNode }) {
const isAuth = useAppSelector(selectIsAuthenticated);
const location = useLocation();
if (!isAuth) {
return <Navigate to="/login" replace state={{ from: location }} />;
}
return <>{children}</>;
}