{"id":25023677,"url":"https://github.com/neworbit/reauthorize","last_synced_at":"2026-04-27T21:31:27.437Z","repository":{"id":33049344,"uuid":"122638546","full_name":"NewOrbit/reauthorize","owner":"NewOrbit","description":"Package providing a number of tools to help you implement authorization in your react and redux application.","archived":false,"fork":false,"pushed_at":"2023-05-25T10:32:55.000Z","size":86,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":25,"default_branch":"master","last_synced_at":"2025-03-18T13:28:23.596Z","etag":null,"topics":["package"],"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/NewOrbit.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-23T15:28:44.000Z","updated_at":"2023-05-22T09:11:40.000Z","dependencies_parsed_at":"2024-11-16T03:24:45.962Z","dependency_job_id":"136fe4fb-33b2-47de-9b36-1704c8c944b8","html_url":"https://github.com/NewOrbit/reauthorize","commit_stats":{"total_commits":41,"total_committers":6,"mean_commits":6.833333333333333,"dds":0.7073170731707317,"last_synced_commit":"fa61f6488d8d353bd05c3642dfae938cd1798ebb"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NewOrbit%2Freauthorize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NewOrbit%2Freauthorize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NewOrbit%2Freauthorize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NewOrbit%2Freauthorize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NewOrbit","download_url":"https://codeload.github.com/NewOrbit/reauthorize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246323485,"owners_count":20758973,"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":["package"],"created_at":"2025-02-05T15:18:40.986Z","updated_at":"2026-04-27T21:31:27.398Z","avatar_url":"https://github.com/NewOrbit.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# reauthorize\n\nThis package provides a number of tools to help you implement authorization in your react and redux application.\n\n## Installation\n\n`npm install reauthorize`\n\n## Key principals\n\nThis package defines an interface `User` which all of the authorization tools expect.\n```ts\ninterface User {\n  authenticated: boolean;\n  roles: string[];\n}\n```\n\nYou can extend this for your own user object.\n\nAll of the below tools use an `AuthorizationSetting` type which is equivalent to: `bool | string | string[]`.  The possible values have the following meanings:\n - `true` - will return `Authorized` for any authenticated user\n - `false` - will return `Authorized` for any user\n - `string` - will return `Authorized` for any authenticated user with a role which matches this string\n - `string[]` - will return `Authorized` for any authenticated user with a role which matches one of the roles in this array\n - `undefined` - will return `Unauthorized` for any user (in case you do not provide a setting)\n\n## `authMiddleware`\n\nThis is a middleware that allows you to authorize based on redux actions.\n\nIts configured with the following options:\n\n```ts\nexport interface AuthMiddlewareOptions\u003cTState, TAction\u003e {\n    actionType: string;                                           // name of action to monitor\n    getUser: (state: TState) =\u003e User;                             // method to get the current user from the state\n    getAuthPayload: (action: TAction) =\u003e AuthPayload;             // method to get the payload from the action\n    unauthorizedAction: any;                                      // action to dispatch if unauthorized\n    unauthenticatedAction?: any;                                  // action to dispatch if unauthenticated (unauthorized will be used if undefined)\n    unauthorizedError?: string;                                   // error message to throw when unauthorized\n    unauthenticatedError?: string;                                // error message to throw when authenticated\n    handleUnauthenticatedApiErrors?: boolean | ShouldHandleError; // handle unauthenticated errors from api requests (see below)\n    handleUnauthorizedApiErrors?: boolean | ShouldHandleError;    // handle unauthorized errors from api requests (see below)\n}\n```\n\nThe `AuthPayload` type is defined as:\n```ts\nexport type AuthPayload = {\n    authorize: AuthorizationSetting,\n    parent?: AuthPayload\n};\n```\n\nSo if parent is defined it will recurse and inherit authorization settings if not defined at the current level.\n\nFor example to use with redux-little-router to authorize your location changes:\n```ts\nimport { LOCATION_CHANGED, replace, Location } from \"redux-little-router\";\nimport { configureAuthMiddleware, AuthState, AuthPayload } from \"reauthorize\";\n\nconst authMiddleware = configureAuthMiddleware\u003cAuthState, { payload: Location }\u003e({\n    actionType: LOCATION_CHANGED,\n    getAuthPayload: action =\u003e (action.payload || {}).result as AuthPayload,\n    getUser: state =\u003e state.currentUser,\n    unauthorizedAction: replace(\"/forbidden\")\n});\n\n// add authorize to your routes:\nconst routes = {\n  \"/home\": {\n    authorize: false\n  },\n  \"/admin\": {\n    authorize: \"ADMIN\"\n  }\n};\n```\n\n### Handle unauthenticated/unauthorized API responses\n\nIf enabled and the next middleware in the chain calls an API and throws an error then the appropriate actions will be dispatched.\n\nIf `handleUnauthenticatedApiErrors` is true then the reauthorize middleware will look for `error.response.status === 401` and dispatch the `unauthenticatedAction` and throw the `unauthenticatedError`.\n\nIf `unauthorizedAction` is true then the reauthorize middleware will look for `error.response.status === 401` and dispatch the `unauthenticatedAction` and throw the `unauthorizedError`.\n\nAlternatively you can provide a `ShouldHandleError` function for either which takes the form `(error: any) =\u003e boolean` to determine whether we want to treat the error as unauthenticated/unauthorized.\n\n\u003e Note if you are using this to redirect to another route, and you are also using middleware to dispatch actions based on your current route you will want to make sure that you don't dispatch those actions if you aren't authenticated\n\n## `Authorize` component\n\nThis is a component you can use to hide parts of a component based on authorization.\n\n\u003e Note: if you need to hide an entire component a better solution is to use the higher order component described below\n\n```ts\nimport * as React from \"react\";\nimport { Authorize } from \"reauthorize\";\n\nclass MyComponent extends React.Component\u003c{}\u003e {\n  public render() {\n    return (\n      \u003cdiv\u003e\n        \u003ch1\u003eMy component\u003c/h1\u003e\n        \u003cp\u003eSome non sensitive information\u003c/p\u003e\n        \u003cAuthorize authorize=\"ADMIN\"\u003e\n          \u003cp\u003eSome sensitive information\u003c/p\u003e\n        \u003c/Authorize\u003e\n      \u003c/div\u003e\n    );\n  }\n}\n```\n\n## `authorize` higher order component\n\nThis is a higher order component, connected to the redux store which allows you to show or hide an entire component based on an authorization setting:\n\n```ts\nimport * as React from \"react\";\nimport { authorize } from \"reauthorize\";\n\nclass MyComponent extends React.Component\u003c{}\u003e {\n  public render() {\n    return \u003cdiv\u003eSome sensitive information\u003c/div\u003e;\n  }\n}\n\nexport default authorize(\"ADMIN\")(MyComponent);\n```\n\n## `isAuthorized` function\n\nThis is the backbone to all of the above tools.  It takes in a `User` and  an `AuthorizationSetting` and will return one of the following results:\n - `\"Authorized\"` - the user is authorized\n - `\"Unauthorized\"` - the user is not authorized\n - `\"Unauthenticated\"` - the user is not authenticated\n\n## License\n\nMade with :sparkling_heart: by [NewOrbit](https://www.neworbit.co.uk/) in Oxfordshire, and licensed under the [MIT Licence](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneworbit%2Freauthorize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fneworbit%2Freauthorize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneworbit%2Freauthorize/lists"}