react-jwt
Small library for decoding json web tokens (JWT)
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);
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);
}
return (
<div>
...
</div>
);
};
License
MIT © @gustavo0197