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

jwt-user-helper

v1.0.4

Published

JWT Token manager with local storage and user helpers

Readme

jwt-user-helper

A lightweight, browser-friendly utility for handling JWT token generation, verification, storage in localStorage, and current user extraction. Designed for React, Next.js, or any frontend JavaScript project.


Features

  • Generate JWT tokens
  • Verify and decode tokens
  • Save and retrieve tokens from localStorage
  • Get current user info from JWT payload
  • Logout / remove token
  • Token expiry checker
  • Works only in the browser (safe for SSR)

Installation

npm install jwt-user-helper

or

yarn add jwt-user-helper


Usage

Usage in Node.js | Server-side | React | Next.js

STEP 1: Mock localStorage BEFORE using the library

global.localStorage = {
  _data: {},
  setItem(key, value) {
    this._data[key] = value;
  },
  getItem(key) {
    return this._data[key];
  },
  removeItem(key) {
    delete this._data[key];
  },
};

STEP 2: Import the library

const {
  generateToken,
  saveToken,
  getCurrentUser,
  isAuthenticated,
  isTokenExpired,
  removeToken,
} = require("jwt-user-helper");

STEP 3: Define a secure secret key

const secret = "my_super_secure_secret_123";

STEP 4: Generate token with user payload and 1 hour expiration

const token = generateToken({ user: { id: 123, name: "Tariq" } }, secret, "1h");
console.log("Generated Token:", token);

STEP 5: Save token to localStorage

saveToken(token);
console.log("Token Saved:", token);

STEP 6: Get user from stored token

console.log("User:", getCurrentUser(secret)); // => { id: 123, name: "Tariq" }

STEP 7: Check authentication status

console.log("Is Authenticated?", isAuthenticated(secret)); // => true 

STEP 8: Check if token is expired

console.log("Is Token Expired?", isTokenExpired()); // => false

STEP 9: Logout (removes token)

removeToken();
console.log("Token Removed:", getCurrentUser(secret)); // => null

Usage in Browser

import {
  generateToken,
  saveToken,
  getCurrentUser,
  isAuthenticated,
  isTokenExpired,
  removeToken,
} from "jwt-user-helper";

const secret = "your_secure_key";
const token = generateToken({ user: { id: 1, name: "Alice" } }, secret, "1h");

saveToken(token);
console.log(getCurrentUser(secret)); // { id: 1, name: "Alice" }

API Reference

| Function | Description | | ------------------------------------------- | ---------------------------------- | | generateToken(payload, secret, expiresIn) | Generate a JWT token | | verifyToken(token, secret) | Verify and decode JWT | | saveToken(token) | Save to localStorage | | getToken() | Get token from storage | | removeToken() | Delete token | | getCurrentUser(secret) | Extract user from token | | isAuthenticated(secret) | Check if valid token exists | | isTokenExpired() | Returns true if token is expired |

Browser Compatibility

  • Works in all modern browsers.
  • Does not work in Node.js (uses localStorage).

Security

node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"

License

MIT © Tariq Mehmood