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

@around25/jwt-utils

v1.0.1

Published

Perform jwt token operations like store, get, decode, get expiration date, check if expired, validate, remove from storage

Downloads

1,844

Readme

A small package to perform JWT token operations (store, get, decode, get expiration date, check if expired, validate, remove from storage).

Works in React and React Native.

Installation

npm install --save jwt-utils

Usage

Initialize the package and then import in wherever you need it. The constructor takes a single config object as parameter. The storageSystem property is required.

import TokenService from 'jwt-utils'

const TokenUtils = new TokenService({
  storageSystem: window.localStorage
});

export default TokenUtils

In the React environment, it can be either window.localStorage or window.sessionStorage. And it's totally synchronous.

Store

Stores token using the specified storage system. Token must be a string.

// Take a properly formatted JWT token
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEiLCJuYW1lIjoiSm9obiBEb2UiLCJleHAiOjE1MjU3MDAxNjE1NjJ9.qGB98H-4th9E0yTVHH235A4kCgFyKt5jIVgekk4fcp4'

TokenUtils.store(token);

Get

Retrieves the stored token if set, undefined otherwise.

const token = TokenUtils.get();

// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEiLCJuY...

Decode

Decodes a token. A falsy token will return {}.

const decodedToken = TokenUtils.decode(token);

// {
//   id: 1,
//   name: "John Doe",
//   exp: 1525700161562
// }

Get expiration date

Returns expiration date as unix timestamp (ms) or null if the exp property is not defined in the decoded token.

const expirationDate = TokenUtils.getExpirationDate(token);

// 1525700161562

Check if expired

Returns a boolean value specifying if token is expired or not.

const isExpired = TokenUtils.isExpired(token);

Check if valid

Checks if token is valid, simply by checking its existence. You can optionally use a validation function as a secondary param. In that case, the validation function should return a boolean value.

const isValid = TokenUtils.isValid(token, validationFunc);

Remove from storage

Removes token from storage

TokenUtils.remove(token);

Usage with React Native

In the React Native environment, store, get and remove methods return promises (due to the async nature of the storage system).

import { AsyncStorage } from 'react-native'
import TokenService from 'jwt-utils'

export default new TokenService({
  storageSystem: AsyncStorage
});
const result = await TokenUtils.store(token);
// true

const token = await TokenUtils.get();
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEiLCJuY...

External dependencies

This package depends on jwt-decode for token decoding.