{"id":23020564,"url":"https://github.com/dipeshrai123/react-auth-navigation","last_synced_at":"2025-08-14T09:32:21.684Z","repository":{"id":44965383,"uuid":"317756492","full_name":"dipeshrai123/react-auth-navigation","owner":"dipeshrai123","description":"React library for authenticated routes","archived":false,"fork":false,"pushed_at":"2022-07-14T10:28:47.000Z","size":84,"stargazers_count":7,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-12-04T04:02:26.273Z","etag":null,"topics":["authentication","navigation","react"],"latest_commit_sha":null,"homepage":"","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/dipeshrai123.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-02T05:10:14.000Z","updated_at":"2022-05-05T14:37:03.000Z","dependencies_parsed_at":"2022-08-04T02:00:24.391Z","dependency_job_id":null,"html_url":"https://github.com/dipeshrai123/react-auth-navigation","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dipeshrai123%2Freact-auth-navigation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dipeshrai123%2Freact-auth-navigation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dipeshrai123%2Freact-auth-navigation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dipeshrai123%2Freact-auth-navigation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dipeshrai123","download_url":"https://codeload.github.com/dipeshrai123/react-auth-navigation/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229763596,"owners_count":18120482,"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":["authentication","navigation","react"],"created_at":"2024-12-15T12:14:23.023Z","updated_at":"2024-12-15T12:14:23.482Z","avatar_url":"https://github.com/dipeshrai123.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-auth-navigation\n\n\u003e React library for authenticated routes\n\n[![NPM](https://shields.io/npm/v/react-auth-navigation.svg)](https://www.npmjs.com/package/react-auth-navigation) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\n## Install\n\n```bash\n// with npm\nnpm i react-auth-navigation\n\n// with yarn\nyarn add react-auth-navigation\n```\n\n## Why is react-auth-navigation ?\n\nIt is a react library built on top of **react-router-dom**. React Auth Navigation provides us to create an authenticated routes and manages all the complicated routing and authenticating the users in client-side.\n\n## Usage\n\n### Navigation\n\nBefore we dive into creating authenticated routes, we should have some concept of **public**, **private** and **protected** routes.\n\nBut, What exactly are public, private and protected routes ?\n\n- **Public Routes** are those routes which can be accessed with or without login.\n- **Private routes** are those routes which cannot be accessed without login.\n- **Protected routes** are those types of public routes which cannot be accessed if a user is logged in.\n\nNow Lets create authenticated routes.\n\n#### withNavigation()\n\n**withNavigation()** is responsible for managing all routes and userRoles. **withNavigation()** hoc should be exported from root component. It accepts **Component** as _first argument_ and **Configuration Object** as _second argument_.\n\nLet us configure the _second argument_.\n\n- **routerType** _( optional )_ : It can be either \"hash\" or \"browser\". Default \"browser\".\n\n- **publicPaths** accepts an array of object with following keys:\n\n  - **key** _( string ) ( optional )_ : Defines unique key for each navigation route.\n  - **name** _( string )_ : Defines the name for a path and used as a routes key for **useNavigation()** hook keys if key is not passed.\n  - **path** _( string )_ : Defines the path for a component.\n  - **component** _( Component )_ : Defines a component for a path.\n  - **restricted** _( boolean )_ : If **true** then it is **protected** route otherwise **public**.\n  - **subPaths** _( array ) ( optional )_ : Accepts array of object with same as **publicPaths** array. It is used to make sub routes _( full-page routing )_.\n  - **nestedPaths** _( array ) ( optional )_ : Accepts array of object with same as **publicPaths** array. It is used to make nested routes _( component routing )_.\n  - **props** _( any ) ( optional )_ : Defines the props for each route keys.\n\n- **privatePaths** accepts an array of object with following keys:\n\n  - **key** _( string ) ( optional )_ : Defines unique key for each navigation route.\n  - **name** _( string )_ : Defines the name for a path and used as a routes key for **useNavigation()** hook keys if key is not passed.\n  - **path** _( string )_ : Defines the path for a component.\n  - **component** _( Component )_ : Defines a component for a path.\n  - **subPaths** _( array ) ( optional )_ : Accepts array of object with same as **publicPaths** array. It is used to make sub routes _( full-page routing )_.\n  - **nestedPaths** _( array ) ( optional )_ : Accepts array of object with same as **publicPaths** array. It is used to make nested routes _( component routing )_.\n  - **props** _( any ) ( optional )_ : Defines the props for each route keys.\n\n- **userRoles** is used to define the access routes for a particular user roles. accepts an object with following format:\n\n  ```typescript\n  ...\n  userRoles: {\n      ...\n      [userRole: string] : { access: Array\u003cstring\u003e }\n  }\n  ```\n\n**Example**\n\nBasic example of routing.\n\nFirst create **publicPaths**, **privatePaths** and **userRoles**.\n\n```javascript\n// routes.js\n\nimport Page1 from \"./Pages/Page1\";\nimport Page2 from \"./Pages/Page2\";\n\nexport const publicPaths = [\n  {\n    name: \"Public\",\n    path: \"/public\",\n    component: Page1,\n    restricted: true,\n  },\n];\n\nexport const privatePaths = [\n  {\n    name: \"Private\",\n    path: \"/private\",\n    component: Page2,\n  },\n];\n\nexport const userRoles = {\n  user: { access: [\"/public\"] },\n  admin: { access: [\"*\"] }, // '*' defines to give access to all paths.\n};\n```\n\nNow lets use this with **withNavigation()** hoc.\n\n```javascript\n// app.js\nimport React from \"react\";\nimport { withNavigation } from \"react-auth-navigation\";\nimport { publicRoutes, privateRoutes, userRoles } from \"./routes\";\n\nconst App = () =\u003e {\n  return (\n    // ...\n  );\n};\n\nexport default withNavigation(App, {\n  publicPaths,\n  privatePaths,\n  userRoles,\n});\n```\n\nAnd that's it. Its all you should do to define the routes and user-roles.\n\n### Auth\n\nAuth provides 2 different HOCs which handles all the authentications defined by **withNavigation()** HOC.\n\n#### Auth\n\nIt lets you define the current state of a user i.e. ( logged state and logged role ) and allows us to define global state which can be accessed from any component with **useAuth()** hook.\n\nIt accepts two props:\n\n- **config** _( object )_\n\n  You must pass an config object to config prop. Object should be of following shape :\n\n  - **isLoggedIn** _( boolean )_ : Defines logged state of a user.\n  - **userRole** _( string )_ : Defines current role of a user.\n\n- **state** _( object )_\n\n  It can be used as a global state which can accept any object with any keys.\n\n#### Auth.Screens\n\nIt returns all the authenticated screens based on the current state of a user and all the routes provided to **withNavigation()** HOC. Component with **Auth.Provider** hoc should be wrapped with **withNavigation()** hoc.\n\nIt can accepts one optional prop:\n\n- **path** _( string ) ( optional )_\n\nIt is required for nested routes. By default its value is taken as _null_ or _'/'_;\n\n**Auth.Screens** hoc should be wrapped inside **Auth** hoc.\n\n**Example**\n\n```javascript\n// app.js\nimport { withNavigation, Auth } from \"react-auth-navigation\";\nimport { publicPaths, privatePaths, userRoles } from \"./routes\";\n\nconst App = () =\u003e {\n  const [config, setConfig] = useState({ isLoggedIn: false, userRole: \"user\" });\n\n  return (\n    \u003cAuth\n      config={config}\n      state={{\n        logout: () =\u003e {\n          setConfig({ isLoggedIn: false, userRole: \"user\" });\n        },\n      }}\n    \u003e\n      \u003cAuth.Screens /\u003e\n    \u003c/Auth\u003e\n  );\n};\n\nexport default withNavigation(App, {\n  publicPaths,\n  privatePaths,\n  userRoles,\n});\n```\n\n### useNavigation()\n\n**useNavigation()** is a hook which gives access to the navigation object providing you to navigate between different screens, providing you all accessible routes according to the current state of a user ( logged state and logged role ). It is also very useful for a component which is not directly a route defined in public or private paths because it doesn't have access to history prop directly.\n\n**useNavigation()** returns an object with the following properties :\n\n- **navigation** _( object )_\n\n  Object for handling navigation and provides all authenticated routes name and path.\n\n  - **routes** _( object )_ : Object with name key you defined in **publicPaths** and **privatePaths** in **withNavigation()** and values are the object of name and path for a defined key.\n  - **navigate** _( string )_ : Function which takes either string or an object similar to _react-router-dom’s_ history.push() function.\n  - **goBack** _( function )_ : Function which will navigate to the previous screen.\n  - **goForward** _( function )_ : Function which will navigate to the next screen if history is available.\n\n- **history** _( object )_ : History object same as _react-router-doms's_ history object.\n\n- **location** _( object )_ : Location object same as _react-router-dom's_ location object.\n\n- **params** _( object )_ : Params object same as _react-router-dom's_ params object.\n\n**Example**\n\n```javascript\nimport { useNavigation } from \"react-auth-navigation\";\n\nconst { navigation, history, location, params } = useNavigation();\n```\n\n### useAuth()\n\n**useAuth()** is a hook which gives access to the config object and state object defined in **\u003cAuth\u003e** hoc directly. By default it returns an object with **isLoggedIn**, **userRole** and all the keys passed inside the state object.\n\n**Example**\n\n```javascript\nimport { useAuth } from \"react-auth-navigation\";\n\nexport default function() {\n\n    // config and state can be accessed with useAuth()\n    const { isLoggedIn, userRole, logout } = useAuth();\n\n    return () {\n        // ...\n    }\n}\n```\n\n## License\n\nMIT © [dipeshrai123](https://github.com/dipeshrai123)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdipeshrai123%2Freact-auth-navigation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdipeshrai123%2Freact-auth-navigation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdipeshrai123%2Freact-auth-navigation/lists"}