react-jwt-kit

Easily handle JWT auth in React app

Usage no npm install needed!

<script type="module">
  import reactJwtKit from 'https://cdn.skypack.dev/react-jwt-kit';
</script>

README

react-jwt-kit

Easily handle JWT auth in React app

NPM JavaScript Style Guide

Install

npm install --save react-jwt-kit
yarn add react-jwt-kit

Usage

import React, { Component } from 'react'
import { AuthProvider, CookieToken } from 'react-jwt-kit'

const handleRefreshToken = (): Promise<string> => {
  return fetch('http://localhost:5000/api/v1/auth/refresh', {
    method: 'POST',
    credentials: 'include'
  }).then((response) => {
    if (response.status === 200) {
      return response.json().then((data) => {
        return data.accessToken
      })
    }
    return undefined
  })
}

function App() {
  return (
    <AuthProvider
      fetchRefreshToken={() => handleRefreshToken()}
      tokenGenerator={() =>
        new CookieToken({
          cookieDomain: window.location.hostname,
          cookieSecure: window.location.protocol === 'https:'
        })
      }
    >
      <Routes />
    </AuthProvider>
  )
}

License

GPL-3.0 © wallforfry