npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-oidc-client

v2.0.0

Published

A declarative approach to working with oidc-client

Downloads

1,534

Readme

react-oidc-client

The repository provides a declarative approach to working with oidc-client. It wraps the different stages of the process in routes and handles everything behind the scenes.

Once authenticated, the children provided to the authentication components are rendered.

Usage

1. Basic Example

import React from "react";
import ReactDOM from "react-dom";
import { Authenticate } from "react-oidc-client";

const MySecretContent = () => <div>My secure content</div>;

ReactDOM.render(
  <Authenticate
    userManagerSettings={{
      loadUserInfo: true,
      userStore: new WebStorageStateStore({
        store: localStorage
      }),
      authority: "http://localhost:5000",
      client_id: "JAVASCRIPT_CLIENT_ID",
      redirect_uri: "http://localhost:3000/login_complete",
      response_type: "id_token token",
      response_mode: "fragment",
      scope: "openid profile", // add other scopes here
      post_logout_redirect_uri: "http://localhost:3000/logout"
    }}
  >
    <MySecretContent />
  </Authenticate>
);

2. Custom Login Complete and Logout Paths

import React from "react";
import ReactDOM from "react-dom";
import { Authenticate } from "react-oidc-client";

const MySecretContent = () => <div>My secure content</div>;

ReactDOM.render(
  <Authenticate
    loginCompletePath="/my_login_complete_path"
    logoutPath="/my_logout_path"
    userManagerSettings={{
      loadUserInfo: true,
      userStore: new WebStorageStateStore({
        store: localStorage
      }),
      authority: "http://localhost:5000",
      client_id: "JAVASCRIPT_CLIENT_ID",
      redirect_uri: "http://localhost:3000/my_login_complete_path",
      response_type: "id_token token",
      response_mode: "fragment",
      scope: "openid profile", // add other scopes here
      post_logout_redirect_uri: "http://localhost:3000/my_logout_path"
    }}
  >
    <MySecretContent />
  </Authenticate>
);

3. Custom Loading Component

import React from "react";
import ReactDOM from "react-dom";
import { Authenticate } from "react-oidc-client";

const MySecretContent:React.FC = () => <div>My secure content</div>;
const LoadingComponent:React.FC = ()=<div>My loader</div>

ReactDOM.render(
  <Authenticate
    LoadingComponent={LoadingComponent}
    loginCompletePath="/my_login_complete_path"
    logoutPath="/my_logout_path"
    userManagerSettings={{
      loadUserInfo: true,
      userStore: new WebStorageStateStore({
        store: localStorage
      }),
      authority: "http://localhost:5000",
      client_id: "JAVASCRIPT_CLIENT_ID",
      redirect_uri: "http://localhost:3000/my_login_complete_path",
      response_type: "id_token token",
      response_mode: "fragment",
      scope: "openid profile", // add other scopes here
      post_logout_redirect_uri: "http://localhost:3000/my_logout_path"
    }}
  >
    <MySecretContent />
  </Authenticate>
);

4. Access Loggedin User Info

import React from "react";
import ReactDOM from "react-dom";
import { Authenticate, useUserIdentity } from "react-oidc-client";

const MySecretContent:React.FC = () => {
  const user = useUserIdentity();
  return <div>{user.profile.name}</div>
};
const LoadingComponent:React.FC = ()=<div>My loader</div>

ReactDOM.render(
  <Authenticate
    LoadingComponent={LoadingComponent}
    loginCompletePath="/my_login_complete_path"
    logoutPath="/my_logout_path"
    userManagerSettings={{
      loadUserInfo: true,
      userStore: new WebStorageStateStore({
        store: localStorage
      }),
      authority: "http://localhost:5000",
      client_id: "JAVASCRIPT_CLIENT_ID",
      redirect_uri: "http://localhost:3000/my_login_complete_path",
      response_type: "id_token token",
      response_mode: "fragment",
      scope: "openid profile", // add other scopes here
      post_logout_redirect_uri: "http://localhost:3000/my_logout_path"
    }}
  >
    <MySecretContent />
  </Authenticate>
);

5. Using create-react-app with a non-root relative path

When using create-react-app, one might use the homepage property if the application isn't hosted at the root of the server. See here for more info. If that's the case, you need provide the base name (as specified in the homepage property in order for the user to be redirected back to the appropraite page, as otherwise, the user will be redirected to paths relative to the root. See the example below:

import React from "react";
import ReactDOM from "react-dom";
import { Authenticate, useUserIdentity } from "react-oidc-client";
const MySecretContent:React.FC = () => {
  const user = useUserIdentity();
  return <div>{user.profile.name}</div>
};
const LoadingComponent:React.FC = ()=<div>My loader</div>
ReactDOM.render(
  <Authenticate
    basename="/myfolderpath"
    LoadingComponent={LoadingComponent}
    loginCompletePath="/my_login_complete_path"
    logoutPath="/my_logout_path"
    userManagerSettings={{
      loadUserInfo: true,
      userStore: new WebStorageStateStore({
        store: localStorage
      }),
      authority: "http://localhost:5000",
      client_id: "JAVASCRIPT_CLIENT_ID",
      redirect_uri: "http://localhost:3000/my_login_complete_path",
      response_type: "id_token token",
      response_mode: "fragment",
      scope: "openid profile", // add other scopes here
      post_logout_redirect_uri: "http://localhost:3000/my_logout_path"
    }}
  >
    <MySecretContent />
  </Authenticate>
);

6. Usage as an alternative to msal to work with Azure B2C

import React from "react";
import ReactDOM from "react-dom";
import { Authenticate, useUserIdentity } from "react-oidc-client";
const MySecretContent:React.FC = () => {
  const user = useUserIdentity();
  return <div>{user.profile.name}</div>
};
const LoadingComponent:React.FC = ()=<div>My loader</div>
const adTenant = "ad_tenant";
const appName = "myApp";
const userSignInFlow = "B2C_1A_signup_signin";
const adResoureceId = `https://${adTenant}.onmicrosoft.com/${appName}`
ReactDOM.render(
  <Authenticate
    basename="/myfolderpath"
    LoadingComponent={LoadingComponent}
    loginCompletePath="/my_login_complete_path"
    logoutPath="/my_logout_path"
    userManagerSettings={{
            client_id: window.appConfig.ad_clientId,
            authority: `https://${adTenant}.b2clogin.com/${adTenant}.onmicrosoft.com/${userSignInFlow}/v2.0`,
            redirect_uri: `http://localhost:3000/login_complete`,
            popup_redirect_uri: `http://localhost:3000/login_complete`,
            response_type: "token id_token",
            automaticSilentRenew: true,
            response_mode: "fragment",
            scope: [
              `${adResourceId}/user_impersonation`,
              "profile",
              "openid"
            ].join(" "),
            post_logout_redirect_uri: `http://localhost:3000/logout`,
            loadUserInfo: false
          }}
  >
    <MySecretContent />
  </Authenticate>
);