{"id":20825826,"url":"https://github.com/upstatement/react-router-guards","last_synced_at":"2025-07-27T21:36:08.276Z","repository":{"id":45432657,"uuid":"185054761","full_name":"Upstatement/react-router-guards","owner":"Upstatement","description":"Guard middleware for React Router navigation","archived":false,"fork":false,"pushed_at":"2021-12-14T09:57:03.000Z","size":6026,"stargazers_count":131,"open_issues_count":16,"forks_count":19,"subscribers_count":15,"default_branch":"main","last_synced_at":"2025-03-30T21:08:07.544Z","etag":null,"topics":["hooks","reactjs","rollup","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/react-router-guards","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Upstatement.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-05T16:18:08.000Z","updated_at":"2025-03-13T14:04:54.000Z","dependencies_parsed_at":"2022-08-04T15:00:31.654Z","dependency_job_id":null,"html_url":"https://github.com/Upstatement/react-router-guards","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Upstatement%2Freact-router-guards","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Upstatement%2Freact-router-guards/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Upstatement%2Freact-router-guards/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Upstatement%2Freact-router-guards/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Upstatement","download_url":"https://codeload.github.com/Upstatement/react-router-guards/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247601448,"owners_count":20964864,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hooks","reactjs","rollup","typescript"],"created_at":"2024-11-17T23:06:54.010Z","updated_at":"2025-04-07T06:11:17.999Z","avatar_url":"https://github.com/Upstatement.png","language":"TypeScript","readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"/static/img/rrg-icon.svg\" width=\"128\" title=\"React Router Guards Logo\" alt=\"Logo\" /\u003e\n\u003c/p\u003e\n\n# react-router-guards\n\n\u003e Guard middleware for React Router navigation\n\nReact Router Guards provides a middleware API for [React Router](https://reacttraining.com/react-router/), allowing you to perform complex logic between the call for navigation and the final render of a route.\n\n## Table of Contents\n\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Basic usage](#basic-usage)\n- [Concepts](#concepts)\n- [Demos](#demos)\n  - [Basic](#basic)\n  - [Intermediate](#intermediate)\n- [Contributing](#contributing)\n- [Code of Conduct](#code-of-conduct)\n- [About Upstatement](#about-upstatement)\n\n## Requirements\n\nThis package has the following [peer dependencies](https://docs.npmjs.com/files/package.json#peerdependencies):\n\n- [React](https://www.npmjs.com/package/react) v16.8.0+ (for hooks ⚓️)\n\n- [React Router DOM](https://www.npmjs.com/package/react-router-dom) v5.0.0+\n\n## Installation\n\nWith [npm](https://www.npmjs.com):\n\n```shell\n$ npm install react-router-guards\n```\n\nWith [yarn](https://yarnpkg.com/):\n\n```shell\n$ yarn add react-router-guards\n```\n\nThen with a module bundler like [webpack](https://webpack.github.io/), use as you would anything else:\n\n```js\n// using ES6 modules\nimport { GuardProvider, GuardedRoute } from 'react-router-guards';\n\n// using CommonJS modules\nconst GuardProvider = require('react-router-guards').GuardProvider;\nconst GuardedRoute = require('react-router-guards').GuardedRoute;\n```\n\n## Basic usage\n\nHere is a very basic example of how to use React Router Guards.\n\n```jsx\nimport React from 'react';\nimport { BrowserRouter } from 'react-router-dom';\nimport { GuardProvider, GuardedRoute } from 'react-router-guards';\nimport { About, Home, Loading, Login, NotFound } from 'pages';\nimport { getIsLoggedIn } from 'utils';\n\nconst requireLogin = (to, from, next) =\u003e {\n  if (to.meta.auth) {\n    if (getIsLoggedIn()) {\n      next();\n    }\n    next.redirect('/login');\n  } else {\n    next();\n  }\n};\n\nconst App = () =\u003e (\n  \u003cBrowserRouter\u003e\n    \u003cGuardProvider guards={[requireLogin]} loading={Loading} error={NotFound}\u003e\n      \u003cSwitch\u003e\n        \u003cGuardedRoute path=\"/login\" exact component={Login} /\u003e\n        \u003cGuardedRoute path=\"/\" exact component={Home} meta={{ auth: true }} /\u003e\n        \u003cGuardedRoute path=\"/about\" exact component={About} meta={{ auth: true }} /\u003e\n        \u003cGuardedRoute path=\"*\" component={NotFound} /\u003e\n      \u003c/Switch\u003e\n    \u003c/GuardProvider\u003e\n  \u003c/BrowserRouter\u003e\n);\n```\n\nCheck out our [demos](#demos) for more examples!\n\n## Concepts\n\n### [Navigation lifecycle](/docs/navigation-lifecycle.md)\n\n\u003e With the addition of guard middleware, the navigation lifecycle has changed.\n\n### [Guard functions](/docs/guard-functions.md)\n\n\u003e Guard functions are the middleware between navigation and rendering.\n\n### [Page components](/docs/page-components.md)\n\n\u003e Page components are used for setting loading and error pages.\n\n### [Guard Provider](/docs/guard-provider.md)\n\n\u003e The `GuardProvider` component is a high-level wrapper for your entire routing solution.\n\n### [Guarded Route](/docs/guarded-route.md)\n\n\u003e The `GuardedRoute` component acts as a replacement for the default [`Route`](https://reacttraining.com/react-router/core/api/Route) component provided by React Router, allowing for routes to use guard middleware.\n\n## Demos\n\nWe've included some demos below to help provide more context on how to use this package!\n\n### Basic\n\n[Demo + Source](https://codesandbox.io/s/react-router-guards-basic-demo-0m48v)\n\nThe basic demo showcases some basic functionality of route guard API with an auth example.\n\n### Intermediate\n\n[Demo](https://react-router-guards-demo.netlify.com) | [Source](demos/intermediate)\n\nThe intermediate demo uses the [PokéAPI](https://pokeapi.co/) to showcase how to use route guards for fetching data from an API.\n\n## Contributing\n\nWe welcome all contributions to our projects! Filing bugs, feature requests, code changes, docs changes, or anything else you'd like to contribute are all more than welcome! More information about contributing can be found in the [contributing guidelines](.github/CONTRIBUTING.md).\n\n## Code of Conduct\n\nUpstatement strives to provide a welcoming, inclusive environment for all users. To hold ourselves accountable to that mission, we have a strictly-enforced [code of conduct](CODE_OF_CONDUCT.md).\n\n## About Upstatement\n\n[Upstatement](https://www.upstatement.com/) is a digital transformation studio headquartered in Boston, MA that imagines and builds exceptional digital experiences. Make sure to check out our [services](https://www.upstatement.com/services/), [work](https://www.upstatement.com/work/), and [open positions](https://www.upstatement.com/jobs/)!\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fupstatement%2Freact-router-guards","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fupstatement%2Freact-router-guards","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fupstatement%2Freact-router-guards/lists"}