New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@around25/jwt-utils

Package Overview
Dependencies
Maintainers
8
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@around25/jwt-utils

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

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
8
Created
Source

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.

Keywords

FAQs

Package last updated on 08 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc