File size: 550 Bytes
b5a1b3b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import { useAuth } from "../context/AuthContext.jsx";
import { Navigate, useLocation } from "react-router-dom";
export default function ProtectedRoute({ children }) {
const { isAuthenticated, token } = useAuth();
const location = useLocation();
// Check both React state and localStorage to prevent transition flashes
const hasToken = isAuthenticated || Boolean(token) || Boolean(localStorage.getItem("diffweave_token"));
if (!hasToken) {
return <Navigate to="/login" state={{ from: location }} replace />;
}
return children;
}
|