Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-jwt

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-jwt

Small library for decoding json web tokens (JWT)

  • 1.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

react-jwt

Small library for decoding json web tokens (JWT)

NPM

Install

npm install react-jwt
or
yarn add react-jwt

Usage

import React from "react";
import { useJwt } from "react-jwt";
const token = "Your JWT";

const Example = () => {
  const { decodedToken, isExpired } = useJwt(token);
  /*
    If is a valid jwt, 'decodedToken' will be a object
    it could look like:
    {
      "name": "Gustavo",
      "iat": 1596408259,
      "exp": 4752168259
    }

    'isExpired' will return a boolean
    true => your token is expired
    false => your token is not expired
  */

  return (
    <div>
      ...
    </div>
  );
};

You can also use the methods isExpired(token) and decodeToken(token)

import React from "react";
import { isExpired, decodeToken } from "react-jwt";
const token = "Your JWT";

const Example = () => {
  const myDecodedToken = decodeToken(token);
  const isMyTokenExpired = isExpired(token);

  return (
    <div>
      ...
    </div>
  );
};

Refresh state

If you use the reEvaluateToken(newToken) method, useJwt's state will be updated

import React from "react";
import { useJwt } from "react-jwt";
const token = "Your JWT";

const Example = () => {
  const { decodedToken, isExpired, reEvaluateToken } = useJwt(token);

  const updateToken = () => {
    const newToken = "A new JWT";
    reEvaluateToken(newToken); // decodedToken and isExpired will be updated
  }

  return (
    <div>
      ...
    </div>
  );
};

License

MIT © @gustavo0197

Keywords

FAQs

Package last updated on 24 Jul 2024

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