1.0.3 • Published 4 years ago

react-oidc v1.0.3

Weekly downloads
2,332
License
MIT
Repository
github
Last release
4 years ago

React OIDC

Wrapper for oidc-client-js, to be used in React apps.

Quick start

You should read the slow start and Routing Considerations too

yarn add react-oidc

You will need the config for the UserManager class.

Example using react-router

import { makeAuthenticator, makeUserManager, Callback } from 'react-oidc'

// supply this yourself
import App from '../layouts/App'
import userManagerConfig from '../config'

const userManager = makeUserManager(userManagerConfig)
const AppWithAuth = makeAuthenticator({
  userManager: userManager,
  signinArgs: {
    state: {
      foo: 15
    }
  }
})(App)

export default () => (
  <Router>
    <Switch>
      <Route
        path="/callback"
        render={routeProps => (
          <Callback
            onSuccess={user => {
              // `user.state` will reflect the state that was passed in via signinArgs.
              routeProps.history.push('/')
            }}
            userManager={userManager}
          />
        )}
      />
      <AppWithAuth />
    </Switch>
  </Router>
)

Slow start

There are 3 main parts to this library:

  • makeUserManager function;
  • makeAuthenticator function;
  • Callback component

makeUserManager(config)

ParamTypeRequiredDefault ValueDescription
configobject (UserManagerSettings)YesundefinedConfig object to pass to UserManager

Helper utility to create a UserManager instance.

makeAuthenticator(params)(<ProtectedApp />)

ParamTypeRequiredDefault ValueDescription
userManagerUserManagerYesundefinedUserManager instance (the result of makeUserManager())
placeholderComponentComponentNonullOptional component to render while auth state is being retrieved

This is a higher-order function that accepts a UserManager instance, and optionally a placeholder component to render when user auth state is being retrieved. It returns a function that accepts a React component. This component should contain all components that you want to be protected by your authentication. Ultimately you will get back a component that either renders the component you passed it (if the user is authenticated), or redirects to the OIDC login screen as defined by the Identity Provider.

The lifecycle of this component is as follows:

  1. The component is constructed with a fetching flag set to true.

  2. On mount, the .getUser() method from UserManager is called. If the user is already authenticated, it will set the fetching flag to false and render the component you passed it.

  3. If the user is not authenticated or their token has expired, the user will be redirected to the login URL (defined by the Identity Provider) and the fetching flag will be set to false.

  4. Upon successful authentication with the Identity Provider, the user will be redirected to the redirect_uri. You should render the Callback component at this location.

Note on the fetching flag

The fetching flag is set to true initially because of the asynchronous nature of .getUser(). There is a need to ensure that we do not redirect to the login page whilst getUser is resolving. Without some way of knowing when the user auth state query is complete, we would end up always redirecting to the login page.

<Callback />

PropTypeRequiredDefault ValueDescription
userManagerUserManagerYesundefinedUserManager instance (the result of makeUserManager())
childrenComponentNonullOptional component to render at the redirect page
onErrorfunctionNoundefinedOptional callback if there is an error from the Promise returned by .signinRedirectCallback()
onSuccessfunctionNoundefinedOptional callback when the Promise from .signinRedirectCallback() resolves

The Callback component will call the .signinRedirectCallback() method from UserManager and if successful, call the onSuccess prop. On error it will call the onError prop. You should pass the same instance of UserManager that you passed to makeAuthenticator.

<UserData />

This component exposes the data of the authenticated user. If you are familiar with React's Context API (the official v16.3.x one), this component is just a Context.

<UserData.Consumer>
  {context => <p>{context.user.id_token}</p>}
</UserData.Consumer>

Render prop function

Argument (key of context)TypeDescription
signOutfunctionCall this to sign the current user out
userUserThis is the User object from oidc-client
userManagerUserManagerUserManager instance from oidc-client

Routing considerations

This library is deliberately unopinionated about routing, however there are restrictions from the oidc-client library that should be considered.

  1. There will be url redirects. It is highly recommended to use a routing library like react-router to help deal with this.

  2. The redirect_uri should match eagerly. You should not render the result of makeAuthenticator()() at the location of the redirect_uri. If you do, you will end up in a redirect loop that ultimately leads you back to the authentication page. In the quick start above, a Switch from react-router is used, and the Callback component is placed before AppWithAuth. This ensures that when the user is redirected to the redirect_uri, AppWithAuth is not rendered. Once the user data has been loaded into storage, onSuccess is called and the user is redirected back to a protected route. When AppWithAuth loads now, the valid user session is in storage and the protected routes are rendered.

1.0.3

4 years ago

1.0.2

4 years ago

1.0.0

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.4.0-alpha.0

5 years ago

0.3.0

5 years ago

0.2.1

5 years ago

0.2.0-0

5 years ago

0.1.2

5 years ago

0.1.2-0

5 years ago

0.1.1

5 years ago

1.0.1

5 years ago

0.1.0

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago