{"id":20388190,"url":"https://github.com/mrbr/react-router-protected-switch","last_synced_at":"2026-07-12T17:01:14.543Z","repository":{"id":57343858,"uuid":"324537071","full_name":"MrBr/react-router-protected-switch","owner":"MrBr","description":"React Router Protected Context","archived":false,"fork":false,"pushed_at":"2021-12-09T11:21:50.000Z","size":8,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-24T03:46:28.008Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MrBr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-26T11:06:07.000Z","updated_at":"2021-12-09T11:21:53.000Z","dependencies_parsed_at":"2022-09-12T07:00:33.083Z","dependency_job_id":null,"html_url":"https://github.com/MrBr/react-router-protected-switch","commit_stats":null,"previous_names":["mrbr/react-router-protected-context"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrBr%2Freact-router-protected-switch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrBr%2Freact-router-protected-switch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrBr%2Freact-router-protected-switch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrBr%2Freact-router-protected-switch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MrBr","download_url":"https://codeload.github.com/MrBr/react-router-protected-switch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241940529,"owners_count":20045878,"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":[],"created_at":"2024-11-15T03:07:47.774Z","updated_at":"2026-07-12T17:01:14.476Z","avatar_url":"https://github.com/MrBr.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# React Router Protected Switch\n\nA generic way for protecting/scoping react router routes with zero dependencies.\n\n## Installation\n\n`yarn add react-router-protected-switch`\n\n## The problem\n\nThere are few common mistakes done with custom protected routes which this library aims to resolve.\n* Validation happens even when the path isn't matched\n* Validation is strongly related to the authorization\n* Validation can't be scoped to the subtree\n\n## The solution\nBy connecting `Route` with `SwitchContext` we get the new `ProtectedRoute` component which doesn't change the `Route` \nprops interface (remains agnostic regarding protection; doesn't require any) but to redirect unwanted visits.\n\n## API\n* `SwitchContext` - the context provider for the custom switch components\n* `switchContext` - under the hood context\n* `SwitchContextConsumer` - the context consumer for the custom protected routes\n* `useSwitchContext` - useful for creating custom protected routes\n\n\n## Usage example:\nSwitch component:\n```\nconst AuthSwitch = ({ isAuthenticated, children }) =\u003e {\n  const redirect = !isAuthenticated \u0026\u0026 '/login';\n\n  return (\n    \u003cSwitchContext value={redirect}\u003e\n      {children}\n    \u003c/SwitchContext\u003e\n  );\n};\n```\n\nIn the Router tree:\n```\n\u003cRouter\u003e\n  \u003cAuthSwitch\u003e\n    // Scoped validation - all routes under the AuthSwitch are protected \n    \u003cProtectedRoute component={SafePage} path=\"/safe-page\"\u003e\n  \u003c/AuthSwitch\u003e\n  // Routes outside the switch behave normally\n  \u003cProtectedRoute component={LoginPage} path=\"/login\"\u003e\n\u003c/Router\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrbr%2Freact-router-protected-switch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrbr%2Freact-router-protected-switch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrbr%2Freact-router-protected-switch/lists"}