react-jwt
    TypeScript icon, indicating that this package has built-in type declarations

    1.1.3 • Public • Published

    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 refreshToken(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

    Install

    npm i react-jwt

    DownloadsWeekly Downloads

    4,847

    Version

    1.1.3

    License

    MIT

    Unpacked Size

    16.9 kB

    Total Files

    10

    Last publish

    Collaborators

    • gustavo0197