First, we have to know what is JWT? JWT(jsonwebtoken) is npm package which is used to create token when user log-in into our application.

  • the basic structure of token is as below:-

JWT token structure

  • Header:- Header in JWT token contains some metadata about the token. we as users don’t have to write anything into it.
  • Payload:- Payload is the information that we have to add to the token for any further use.
  • Signature:- Signature is the URLencoded text which is made using Header, Payload, and Secret available in the server. the same secret is to be used to extract information from tokens in the future. The Secret is used so that only authorized servers can only decode Token.

Now let us see how we can use tokens in the server.

Step 1:- Installing JWT and setting registration part.

Step2:- Handling login route

Step 3:- Creating Token

Step4:- Returning token where we called createtoken().

Step 5:- using the token to authenticate.

Step 6:- Creating auth.js file into middlewares folder

Step 7:- Using the “isLoggedIn()” method inside the auth.js file when we need authorization.

#nodejs #authentication #jwt #expressjs

JWT(jsonwebtoken) token Based Authentication using NodeJS.
2.05 GEEK