{"id":15462862,"url":"https://github.com/mikeldking/react-auth0-context","last_synced_at":"2025-07-10T02:36:36.935Z","repository":{"id":41652062,"uuid":"260105713","full_name":"mikeldking/react-auth0-context","owner":"mikeldking","description":"React Context and Hook for Auth0 Authentication","archived":false,"fork":false,"pushed_at":"2023-01-06T04:38:52.000Z","size":2776,"stargazers_count":5,"open_issues_count":19,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-22T10:42:59.298Z","etag":null,"topics":["auth0","hooks","react","typescript"],"latest_commit_sha":null,"homepage":null,"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/mikeldking.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-30T03:35:43.000Z","updated_at":"2023-10-03T07:43:06.000Z","dependencies_parsed_at":"2023-02-05T08:16:47.209Z","dependency_job_id":null,"html_url":"https://github.com/mikeldking/react-auth0-context","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeldking%2Freact-auth0-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeldking%2Freact-auth0-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeldking%2Freact-auth0-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeldking%2Freact-auth0-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikeldking","download_url":"https://codeload.github.com/mikeldking/react-auth0-context/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250222323,"owners_count":21394850,"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":["auth0","hooks","react","typescript"],"created_at":"2024-10-02T00:05:23.781Z","updated_at":"2025-04-22T10:43:08.211Z","avatar_url":"https://github.com/mikeldking.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-auth0-context\n\nReact context and hook for Auth0 Authentication using `@auth0/auth0-spa-js`\n\n## Install\n\n```\nyarn add react-auth0-context\n```\n\n## Setup\n\nAdd the AuthProvider at the root of your React app and pass in your Auth0 application info and API identifier (audience)\n\n```typescript\nimport { AuthProvider } from \"react-auth0-context\";\n\nReactDOM.render(\n  \u003cReact.StrictMode\u003e\n    \u003cAuthProvider domain={domain} client_id={client_id} audience={audience}\u003e\n      \u003cApp /\u003e\n    \u003c/AuthProvider\u003e\n  \u003c/React.StrictMode\u003e,\n  document.getElementById(\"root\")\n);\n```\n\nAdd a way to log in\n\n```typescript\nimport { useAuth } from \"react-auth0-context\";\n\nconst LoginButton = () =\u003e {\n  const { isAuthenticated, loginWithRedirect, logout } = useAuth();\n  return (\n    \u003cbutton\n      onClick={() =\u003e {\n        if (isAuthenticated) {\n          logout();\n        } else {\n          loginWithRedirect({\n            redirect_uri: window.location.origin,\n          });\n        }\n      }}\n    \u003e\n      {isAuthenticated ? \"Log Out\" : \"Log In\"}\n    \u003c/button\u003e\n  );\n};\n```\n\nUse the `useAuth` hook to get user info and authentication tokens to talk to your API\n\n```typescript\nconst { user, getTokenSilently } = useAuth();\n\nasync function makeAuthenticatedAPIRequest() {\n  const token = await getTokenSilently();\n  const response = await fetch(url, {\n    headers: {\n      \"Content-Type\": \"application/json\",\n      Authentication: `Bearer ${token}`,\n    },\n    body: data,\n  });\n  return response.json();\n}\n```\n\n## How to use with Routing\nIf you are using something like React Router, you can use `useAuth` to create a private toute\n\n```typescript\nimport React, { useEffect } from \"react\";\nimport { Route, RouteProps } from \"react-router-dom\";\nimport { useAuth } from \"react-auth0-context\";\nimport { Location } from \"history\";\n\ninterface IPrivateRouteProps extends RouteProps {\n  // tslint:disable-next-line:no-any\n  component: any;\n}\n\nfunction locationToString(location: Location\u003cany\u003e) {\n  const { pathname, search } = location;\n  let str = pathname;\n  if (search) {\n    str += search;\n  }\n  return str;\n}\n\nconst PrivateRoute: React.FunctionComponent\u003cIPrivateRouteProps\u003e = (\n  routeProps\n) =\u003e {\n  const { component: Component, path, ...rest } = routeProps;\n  const { isAuthenticated, loginWithRedirect } = useAuth();\n\n  useEffect(() =\u003e {\n    const loginFn = async () =\u003e {\n      if (!isAuthenticated) {\n        const targetUrl =\n          routeProps.location \u0026\u0026 locationToString(routeProps.location);\n        await loginWithRedirect({\n          redirect_uri: window.location.origin,\n          appState: { targetUrl },\n        });\n      }\n    };\n    loginFn();\n    /* eslint-disable-next-line react-hooks/exhaustive-deps */\n  }, [isAuthenticated, loginWithRedirect, path]);\n  return isAuthenticated ? (\n    \u003cRoute path={path} render={(props) =\u003e \u003cComponent {...props} /\u003e} {...rest} /\u003e\n  ) : null;\n};\n\nexport default PrivateRoute;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikeldking%2Freact-auth0-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikeldking%2Freact-auth0-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikeldking%2Freact-auth0-context/lists"}