1.2.2 • Published 5 months ago
react-jwt v1.2.2
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);
/*
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
1.2.2
5 months ago
2.0.0-beta.1
9 months ago
1.2.1
9 months ago
1.2.0
2 years ago
1.1.8
2 years ago
1.1.7
2 years ago
1.1.6
3 years ago
1.1.6-beta.1
3 years ago
1.1.6-beta.2
3 years ago
1.1.5
3 years ago
1.1.4
3 years ago
1.1.3
3 years ago
1.1.2
4 years ago
1.1.1
4 years ago
1.1.0
4 years ago
1.0.9
4 years ago
1.0.8
4 years ago
1.0.7
4 years ago
1.0.5
4 years ago
1.0.4
4 years ago
1.0.3
4 years ago
1.0.1
4 years ago
1.0.0
4 years ago