DEV Community 👩‍💻👨‍💻

Cover image for ECOMMERCE Website Using MERN Part-6.3.1 ( Authentication { Token Generation} )
Bikramjeet Sarmah
Bikramjeet Sarmah

Posted on

ECOMMERCE Website Using MERN Part-6.3.1 ( Authentication { Token Generation} )

In this blog we are gonna set JWT and make save a token when they are logged in so that they can auto login.

Step-1 Import jsonwebtoken in our userModel.js file.
Import

Step-2 Lets make a method named getJWTToken, this will generate our token for user.
Token

Here we use the .sign from jwt and pass the id of the user with a secret key and an expire time which we will be getting from our env file.
Env

Here the secret key can be anything. I have just moved my finger randomly on my keyboard lol.

Step-3 Now lets use this function in our userController inside of the registerUser function at first. Our function shall look like
Register

So our token generation is completed now. In the Next blog we will be making the login function.

Top comments (0)

🌚 Life is too short to browse without dark mode