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)
}
)

1 comment

Recent Posts

See All

Node.js Streams

What are Streams? Streams are objects that let you read data from a source or write data to a destination in continuous fashion. In Node.js, there are four types of streams: Readable: Stream which is

Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

CodersArts is a Product by Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram