JWT authentication in MERN

JWT Authentication

Working of JWT (JSON Web Token)

When user enter his credential and clicks on login 

the server checks if user is authentic or not (this involves checking the entered username and password against a database of user records.)

If it is correct credentials then,

server generates a Token 

Token has 3 parts

header : json format info of token like type and algorithm used in it.

payload : data abu the user so that next time pata chale ke yeh user ka data h

secret key: some text that server only knows so that he can open the token 

server then signs the header and payload with the secret key and this token is sent back to user 

whenever user makes any request to server this token is along with it

so server first checks the token is correct or not then only process request and sends response to user.

  • npm  install jsonwebtoken bcrypt 

bcryptjs is used to hash the passwords in the database

Comments