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

oidc-jwt-client

v4.0.18

Published

Fetch JWTs for API access from oidc-jwt-provider

Downloads

801

Readme

oidc-jwt-client

Fetch JWTs for API access from oidc-jwt-provider

Installation

npm install oidc-jwt-client --save

How to use

<OidcJwtProvider
  client={{ url: 'https://api-auth.acc.titan.awssdu.nl' }}
  shouldAttemptLogin={false}
  shouldMonitorAccessTokens={false}
>
  // Contents of your app
</OidcJwtProvider>

When you come back after authorization to your app it will have a token in the url like this ?token=. To replace this token we use window.history.replaceState() by default. If you would like to replace this behaviour you could send a custom removeTokenFromUrlFunction.

In NextJS you could create a helper function like this:

// removeTokenFromUrlFunction.ts
import Router from 'next/router';
import { stripTokenFromUrl } from 'oidc-jwt-client';

const removeTokenFromUrlFunction = (url: string) => {
  const urlWithoutToken = stripTokenFromUrl(url);
  Router.replace(urlWithoutToken, undefined, { shallow: true });
};

export { removeTokenFromUrlFunction };

And then use it like this:

// App.tsx
<OidcJwtProvider
  client={{ url: 'https://api-auth.ota.titan2.awssdu.nl' }}
  shouldAttemptLogin={false}
  shouldMonitorAccessTokens={false}
  removeTokenFromUrlFunction={removeTokenFromUrlFunction}
>
  // Contents of your app
</OidcJwtProvider>

Fetch an accessToken

Within the provider we make use of several hooks to use the functionality exposed within the context.

The accessToken is directly returned from the fetchAccessToken function when already present and valid. If not it will automatically fetch a new accessToken for you.

To get the accessToken you can do this:

const [token, setToken] = (useState < null) | (string > null);
const fetchAccessToken = useAuthAccessToken();

useEffect(() => {
  fetchAccessToken().then((token) => {
    setToken(token);
  });
}, [fetchAccessToken, setToken]);

Login and Logout functions

To login or logout a user manually you can make use of these two function exposed by the useAuthControls hook:

const { authorize, logout } = useAuthControls();

const onClickLogout = React.useCallback(() => {
  logout();
}, [logout]);

const onClickLogin = React.useCallback(() => {
  authorize();
}, [authorize]);

Check if authentication client has initialized

Checks when the loadInitialData function is done executing and will return true when finished. NB! This doesn't mean you're logged in, jsut that the authentication is done initializing.

const isInitialized = useAuthInitialized();
console.log('Auth is initialized: ', isInitialized);

Get User Info

To get the user info you can do this within the context of the provider:

const { value, loading } = useAuthUserInfo();
console.log('This is the userInfo: ', value);

Get the Claims

To get the claims you can do this within the context of the provider:

const { value, loading } = useAuthAccessClaims();
console.log('These are the claims: ', value);

Check if a user is logged in.

Checking if the user is logged in so that you can act on it.

const isLoggedIn = useAuthIsLoggedIn();
console.log('Is the user loggedin? ', isLoggedIn);

Check if the users session has expired

Checking if the users session has expired

const isSessionExpired = useAuthSessionExpired();
console.log('Is the users session expired? ', isSessionExpired);