top of page

What is JWT?



JWT stands for JSON web Token

  • It is very popular technology to verify the json data of user (User authentication).

  • It is very secure as once it is send to frontend then no one can modify it if someone modify it user lose the access of the information.

  • It is mostly used for rest API authentication.


Now let’s understand it by writing some code.

We will be understanding it by using Node.js

We also need a NPM package called jsonwebtoken


Step1:- Generation a json web token using the user information payload with an expire time.



Importing jwt from package installed

Var jwt = require(‘jsonwebtoken’);

Const payload = {
Name: ‘user’
Username: ‘username’,
}

Jwt.sign(
//injecting payload
{…payoad},
//key
‘authentication’,
{
// the jwt token will be expire in 10hrs.
expiresIn: ‘10hrs’
},
// we will get an error or an token
(err, token) => {
//check if error is there
If(err){
return Console.log(err);
}
Console.log(token);
}
)

Step2:- Method to verify token


Jwt.verify(
Usertoken,
‘authentication’,
(err, decodedToken) => {
If(err){
return console.log(‘unauthorized’)
}
// you will get the same payload object that you have set
return console.log(decodedToken)
}
)

Comments


bottom of page