DEV Community

Antony Raj
Antony Raj

Posted on


First, install the jwt-decode library using npm or yarn:

npm install jwt-decode


yarn add jwt-decode

Now, you can use the jwt-decode library to decode the JWT token in your React component. Here's an example:

`import React, { useEffect } from 'react';

const App = () => {
useEffect(() => {
const accessToken =

const parts = accessToken.split('.');
const decodedPayload = atob(parts[1]);

const parsedPayload = JSON.parse(decodedPayload);

const { exp } = parsedPayload;

const currentTime = Math.floor( / 1000);
console.log("current time",currentTime)

if (exp && currentTime >= exp) {
  console.log('Token has expired. Perform logout action.');
} else {
  console.log('Token is still valid.');
Enter fullscreen mode Exit fullscreen mode

}, []);

return (

Decode JWT Example

Check the console for the decoded token.


export default App;`

In this example, we import the jwtDecode function from the jwt-decode library. We then define a JWT token and use the jwtDecode function to decode the token. The decoded token is logged to the console.

Make sure to replace the accessToken variable with your own JWT token. If the token is valid, the jwtDecode function will decode it and return an object with the token's claims. If the token is invalid, the function will throw an error.

Top comments (0)