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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-token-manager

v1.1.7

Published

A simple React library to manage JWT tokens

Downloads

886

Readme

React Token Manager

A simple React library to manage JWT tokens with support for localStorage, sessionStorage, and cookies. Features include storing tokens, decoding JWTs, checking expiration, and a convenient React hook.


Installation

npm install react-token-manager

Global Configuration

Set storage type and token key once for your app:

import { configureTokenManager } from 'react-token-manager'

configureTokenManager({
  storage: 'localStorage', // 'localStorage' | 'sessionStorage' | 'cookie'
})

Using the Hook useTokenManager

import { useTokenManager } from 'react-token-manager'

export default function Dashboard() {
  const {
    setTokens,
    getTokens,
    getSingleToken,
    removeTokens,
    clearTokens,
    decodeToken,
    isExpired,
  } = useTokenManager()

  // Set multiple tokens
  setTokens({
    access_token: 'abc123',
    refresh_token: 'xyz789',
  })

  // Get multiple tokens
  const tokens = getTokens(['access_token', 'refresh_token'])
  console.log(tokens.access_token) // 'abc123'

  // Get a single token by key
  const accessToken = getSingleToken('access_token')
  console.log(accessToken)

  // Decode a token
  const payload = decodeToken(accessToken!)

  // Check if a token is expired
  console.log(isExpired(accessToken!))

  // Remove multiple tokens
  removeTokens(['access_token', 'refresh_token'])

  // Clear all tracked tokens
  clearTokens()
}

You can override storage or tokenKey per hook if needed.

Using TokenManager Class Directly

import { TokenManager } from 'react-token-manager'

const manager = new TokenManager({ storage: 'cookie' })

// Set multiple tokens
manager.set({
  access_token: 'abc123',
  refresh_token: 'xyz789',
})

// Get multiple tokens
const tokens = manager.get(['access_token', 'refresh_token'])
console.log(tokens.access_token)

// Get a single token
const access = manager.getOne('access_token')

// Decode a token
console.log(manager.decode(access!))

// Check if token is expired
console.log(manager.isExpired(access!))

// Remove multiple tokens
manager.remove(['access_token', 'refresh_token'])

// Clear all tokens
manager.clear()

API Reference

Set global options.

{
  storage?: 'localStorage' | 'sessionStorage' | 'cookie';
}


useTokenManager(options?: TokenManagerOptions)

Returns:
  setTokens(tokens: Record<string, string>)       // Set multiple tokens

  getTokens(keys: string | string[])              // Get multiple tokens

  getSingleToken(key: string)	                    //Get a single token by key

  removeTokens(keys: string | string[])          // Remove multiple tokens

  decodeToken<T = unknown>(token: string)        // Decode a single token

  isExpired(token: string)                       // Check if a token is expired


TokenManager Class Methods
  set(tokens: Record<string, string>)            // Store multiple tokens

  get(keys: string | string[])                   // Retrieve multiple tokens

  getOne(key: string)	                           // Retrieve a single token

  remove(keys: string | string[])                // Delete multiple tokens

  decode<T = unknown>(token: string)             // Decode a single token

  isExpired(token: string)                       // Check if a token is expired

Examples

Multiple Tokens

const { setTokens, getTokens } = useTokenManager()

setTokens({
  access_token: 'abc',
  refresh_token: 'xyz',
})

const tokens = getTokens(['access_token', 'refresh_token'])
console.log(tokens.access_token) // 'abc'
console.log(tokens.refresh_token) // 'xyz'

Using Cookies

configureTokenManager({ storage: 'cookie' })

Checking Expiry Before API Call

const { getSingleToken, isExpired } = useTokenManager()
const accessToken = getSingleToken('access_token')

if (!isExpired(accessToken!)) {
  callApi(accessToken)
} else {
  refreshToken()
}