+ 1

How to use JWT authentication in MERN stack.

Could I know how to use JWT authentication in MERN stack.

10th Dec 2020, 3:17 AM
⚡V.M⚡
⚡V.M⚡ - avatar
2 Respuestas
+ 2
You would want to install and import the jwtwebtoken npm package in your node backend. I typically like to create a separate generateToken() function, which I use to generate a jwt using the .sign(id, secret) method. The id is the user associated with the token and the secret, which can be any string of characters and is used to verify tokens on the server. You can then save the generated key to the document associated with user in the database. Each time a user logs in with a different device, a new token is added to their user document. Lastly, return the token. In the login POST route on express, you can now call the generateToken() function and send that token back as part of the response. Using the fetch API, you can retrieve the token on the front-end and store it in cookies. You can view this article for more information: https://itnext.io/authentication-in-mern-stack-using-jwt-25c966027f77?gi=10d51fea7833
29th Dec 2020, 5:56 PM
Ethan Cannelongo
Ethan Cannelongo - avatar
30th Dec 2020, 6:04 AM
⚡V.M⚡
⚡V.M⚡ - avatar