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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-oidc-pkce

v1.0.5

Published

React Context API provider implementation for Open ID connect Authorization code flow with PKCE

Readme

React Context Provider implementation for OIDC Authorization code Flow with PKCE

This library provides a simple way to implement the OIDC Authorization Code flow with PKCE (Proof Key for Code Exchange) in your React applications using context provider.

Installation

npm install react-oidc-pkce

Usage

1. Initialize the OIDC Context Provider

First, initialize the OIDC context provider in your root component to provide authentication state and actions to child components:

import React from 'react';
import { OIDCContextProvider } from "react-oidc-pkce";

const App = () => {
  const oidcConfig = {
    // Your OIDC configuration options (clientId, redirectUrl, oidcUrl, etc.)
  };

  return (
    <OIDCContextProvider oidcConfig={oidcConfig}>
      <YourAppContent />
    </OIDCContextProvider>
  );
};

export default App;

2. Use the Auth Context in Components

You can use the provided authentication context in your components to access authentication state and perform authorization flow:

import React, { useContext, useState } from 'react';
import { useAuthContext } from 'react-oidc-pkce';

const YourComponent = () => {
  const [isAuthenticated, setAuthenticated] = useState(false)
  const auth = useAuthContext()

  useEffect(() => {
    auth.registerCallback((result) => {
      if (result === 'SUCCESS') {
        setAuthenticated(true)
      } else {
        setAuthenticated(false)
      }
    })
  }, [])

  const handleLogin = () => {
    auth.authorize({ force: false }); // Optionally pass `force: true` to force re-authorization
  };

  
  return (
    <div>
      {isAuthenticated ? (
        <button >Logout</button>
      ) : (
        <button onClick={handleLogin}>Login</button>
      )}
    </div>
  );
};

export default YourComponent;

API Reference

OIDCContextProvider

The OIDCContextProvider component is used to initialize the OIDC context provider in your application.

oidcConfig

Props object containing OIDC configuration options.

const mockOidcConfig: OidcConfig = {
  redirectUrl: 'http://example.com/callback', // OIDC callback url
  autoTokenRefresh: true, // if true, the acces token will be automatically rotated in background
  clientId: "abc", // client id 
  scope: "email", // scope for your access
  oidcUrl: "https://oidc.com/" // your oidc URL
};

useAuthContext

React hook implementation which provides access to authentication state and actions.

authorize(options: any)

Method which intiates the authorzation flow, which accespts an options object with attribute force: true | false to force re-authorization if true.

registerCallback(authCallback: (result: string) => void)

For registering a authorization callback function. If auth operation is success, the specified callback function will be invoked with result SUCCESS else if is a failure then in will be invoked with result FAILED

getAccessToken(): string

Returns a valid access token

getRefreshToken(): string

Returns a refresh token

getIdToken(): string

Returns a ID token

isAuthValid(): boolen

Returns whether current authentication is valid or not

Contributing

Contributions are welcome! Please feel free to open issues or pull requests for any improvements, bug fixes, or new features.