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-jwt-api-adapter

v1.1.0

Published

React context provider for requesting API's with expiring JWT's.

Downloads

10

Readme

react-jwt-api-adapter

React context provider for requesting API's with expiring JWT's. This provider checks the expiration date (exp variable when decoded) inside the JWT and runs the updateToken callback when validation fails. Using the updateToken callback you can request your authorization service for a new token so the next API call will succeed. This package relies on the jsonwebtoken dependency

NPM JavaScript Style Guide

Install

npm install --save react-jwt-api-adapter jsonwebtoken

Usage

import React from 'react'

// Libraries
import ReactJwtApiAdapter, { useJwtApiAdapter } from 'react-jwt-api-adapter'

const App = () => {
  return (
    <ReactJwtApiAdapter
      baseUrl='http://localhost:8000/api/v1'
      token={
        'eyJhbGciOiJSUzI1NiIsInR5cCI6Ikp.eyJpZCI6ImZjMDg0MTI5LTYyNDctNDUyNS.NZfIVPYeVIs-tU2MHWk7tW'
      }
      updateToken={async () => {
        return 'eyJhbGciOiJSUzI1NiIsI.eyJpZCI6ImZjMDg0MTI5LTYyNDc.EqqlHjK4-a5PX9YoVz6pFk86wnF7_Szl_'
      }}
      leeway={5}
    >
      <Profile />
    </ReactJwtApiAdapter>
  )
}

const Profile = () => {
  const adapter: any = useJwtApiAdapter()
  const [user, setUser] = React.useState({})

  React.useEffect(() => {
    const get = async () => {
      try {
        const response = await adapter.callApi(
          '/user',
          { method: 'GET' }
        )

        if (response.request.status === 200) {
          setUser(response.data)
        }
      } catch (error) {
        console.error(error)
      }
    }

    get()
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])

  return (
    <div>
      User: <br />
      {JSON.stringify(user)}
    </div>
  )
}

export default App

Or change the callApi to use the interceptor if you want to manipulate the request function before firing the fetch, this way you can add extra headers (for example)

const response = await adapter.callApi(
  '/user',
  {
    method: 'GET'
  },
  async (url: string, settings: Object) => {
    const interceptorResponse = await adapter.requestApi(url, settings)

    return interceptorResponse
  }
)

Props

Common props you may want to specify:

  • baseUrl - the base URL of the API
  • token - the access token to start with
  • updateToken - callback function for requesting a new access token
  • leeway - optional number in seconds to account for a clock skew times difference between the signing and verifying servers

Context data

Common data you may want to use inside the context:

  • token - the given start access token
  • callApi - function for calling the API with a middleware for checking the expiration date inside the JWT
  • requestApi - immediately call the API without checking the JWT

useJwtApiAdapter

Use the useJwtApiAdapter hook inside the provider to use the context data

License

MIT © lennartkoelewijn