@emmert/auth-client

JWT Token Management

Usage no npm install needed!

<script type="module">
  import emmertAuthClient from 'https://cdn.skypack.dev/@emmert/auth-client';
</script>

README

Auth Client

Tools for managing client-side authentication.

Features

  • Store tokens in localStorage for later use even after browser refresh
  • Automatically try to refresh 3 times before automatically logging the user out
  • Automatically refresh auth tokens on a regular basis to keep them fresh

Installation

NPM
npm install @emmert/auth-client

Yarn
yarn add @emmert/auth-client

Configuration & Setup

import { AuthClient } from "@emmert/auth-client";

const client = new AuthClient({
    getNewToken: async (refreshToken) => {
        const response = await [...code to fetch token here...]
        return {
            accessToken: response.accessToken,
            refreshToken: response.refreshToken
        }
    },
    onLogout: (reason: LogoutReason) => {
        ...handle logout here
    },
    onError:(e) => {
        ...log errors here
    }
});

Reference

Load a new token

client.load();

Start polling for a new token on an interval

client.startPolling(10 * 60 * 1000); // Get a new token ever 10 minutes

Manually stop polling

client.stopPolling();