react-oidc-context

Lightweight auth library based on oidc-client-ts for React single page appl...

README

react-oidc-context


Lightweight auth library using the
oidc-client-ts library for React
single page applications (SPA). Support for
hooks and

Documentation


This library implements an auth context provider by making use of the
oidc-client-ts library. Its configuration is tight coupled to that library.


The
[User](https://authts.github.io/oidc-client-ts/classes/User.html)
and
[UserManager](https://authts.github.io/oidc-client-ts/classes/UserManager.html)
is hold in this context, which is accessible from the
React application. Additionally it intercepts the auth redirects by looking at
the query/fragment parameters and acts accordingly. You still need to setup a
redirect uri, which must point to your application, but you do not need to
create that route.

To renew the access token, the
feature of oidc-client-ts can be used.

Installation


Using npm

  1. ```bash
  2. npm install oidc-client-ts react-oidc-context --save
  3. ```

Using yarn

  1. ```bash
  2. yarn add oidc-client-ts react-oidc-context
  3. ```

Getting Started


Configure the library by wrapping your application in AuthProvider:

  1. ```jsx
  2. // src/index.jsx
  3. import React from "react";
  4. import ReactDOM from "react-dom";
  5. import { AuthProvider } from "react-oidc-context";
  6. import App from "./App";

  7. const oidcConfig = {
  8.   authority: "<your authority>",
  9.   client_id: "<your client id>",
  10.   redirect_uri: "<your redirect uri>",
  11.   // ...
  12. };

  13. ReactDOM.render(
  14.   <AuthProvider {...oidcConfig}>
  15.     <App />
  16.   </AuthProvider>,
  17.   document.getElementById("app")
  18. );
  19. ```

Use the useAuth hook in your components to access authentication state
(isLoading, isAuthenticated and user) and authentication methods
(signinRedirect, removeUser and signOutRedirect):

  1. ```jsx
  2. // src/App.jsx
  3. import React from "react";
  4. import { useAuth } from "react-oidc-context";

  5. function App() {
  6.     const auth = useAuth();

  7.     switch (auth.activeNavigator) {
  8.         case "signinSilent":
  9.             return <div>Signing you in...</div>;
  10.         case "signoutRedirect":
  11.             return <div>Signing you out...</div>;
  12.     }

  13.     if (auth.isLoading) {
  14.         return <div>Loading...</div>;
  15.     }

  16.     if (auth.error) {
  17.         return <div>Oops... {auth.error.message}</div>;
  18.     }

  19.     if (auth.isAuthenticated) {
  20.         return (
  21.         <div>
  22.             Hello {auth.user?.profile.sub}{" "}
  23.             <button onClick={() => void auth.removeUser()}>Log out</button>
  24.         </div>
  25.         );
  26.     }

  27.     return <button onClick={() => void auth.signinRedirect()}>Log in</button>;
  28. }

  29. export default App;
  30. ```

You must provide an implementation of onSigninCallback to oidcConfig to remove the payload from the URL upon successful login. Otherwise if you refresh the page and the payload is still there, signinSilent - which handles renewing your token - won't work.

A working implementation is already in the code here.