Route protection and access management, simplified for React.
RouteKeeper is a React routing utility that manages access and navigation intelligently. It ensures users always reach the right pages based on authentication and roles, simplifying route management and enforcing access control in any application—whether a simple dashboard or a large-scale app.
- Protect routes based on authentication state (
auth) and user roles (userRoles) without writing repetitive logic.
- Restrict routes to users with specific roles.
- Supports inheritance across nested routes for flexible access management.
- Public: Accessible to all users; can redirect authenticated users.
- Private: Requires authentication, with optional role checks.
- Neutral: Always accessible, ignores authentication.
- Seamlessly works with deeply nested route configurations.
- Respects parent roles and route types.
- Automatically redirect users if they try to access unauthorized routes.
- Supports
pathname,search,hash,state,replace,relative, andpreventScrollReset.
- Handles
React.lazyroutes with built-inSuspensefallbacks.
- Loading screen:
loadingScreen - Private route fallback:
privateFallback - Unauthorized access screen:
unAuthorized - Not found page:
notFound
- Wraps your app in an error boundary by default.
- Can be disabled using
disableErrorBoundary.
- onRouteChange: Triggered when the current route changes.
- onRedirect: Triggered whenever a redirect occurs.
- Provides helpful console warnings for misconfigured routes, duplicate paths, invalid redirects, and more.
npm install routekeeper-react
yarn add routekeeper-react
pnpm add routekeeper-reactimport React from "react";
import { BrowserRouter } from "react-router-dom";
import { RouteKeeper } from "routekeeper-react";
import Home from "./pages/Home";
import Login from "./pages/Login";
import LandingPage from "./components/LandingPage";
import { defineRoutes } from 'routekeeper-react';
const userIsLoggedIn = true;
const routes = defineRoutes([
{ path: "/", element: <Home />, type: "private" },
{ path: "/login", element: <Login />, type: "public" }
]);
const App = () => {
return (
<BrowserRouter>
<RouteKeeper
routes={routes}
auth={userIsLoggedIn}
/>
</BrowserRouter>
);
};
export default App;Found a bug or want to add a feature? Contributions are welcome!
- 🍴 Fork it
- 🌟 Star it (pretty please?)
- 🔧 Fix it
- 📤 PR it
- 🎉 Celebrate!
Please ensure your code follows the existing style and includes clear commit messages.
Full docs, examples, and advanced usage are available on the documentation site.
This project is licensed under the MIT License – see the LICENSE file for details.
Built by Isaac Anasonye, designed to simplify and standardize routing in React applications.
RouteKeeper – Protecting your routes since 2025!
Made something awesome with RouteKeeper?
⭐ Star on GitHub | 📢 Share on Twitter | 💬 Join the Discussion | 🔗 Connect on LinkedIn