To generate a random JWT secret key, you can use a tool like Node.js to create a random string. Here's a simple example:
- Open your terminal or command prompt.
- Run the following Node.js script to generate a random string:
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
This command uses the crypto module in Node.js to generate a random sequence of 32 bytes and then converts it to a hexadecimal string.
Copy the generated string.
Open your
.envfile and set the JWT secret key:
JWT_SECRET=paste-the-generated-string-here
Replace paste-the-generated-string-here with the string you copied.
Save the changes to your .env file.
Now, you have a securely generated JWT secret key. Remember to keep this key confidential and don't share it publicly. If needed, you can regenerate the key and update it in your .env file.
Top comments (19)
Thanks for this mate
Thanks!
Thanks!!!
Helpful in my job!
merci cela m'a beaucoup aidé
Bienvenue 😀 N'hésitez pas à le partager avec vos amis.
Aaahh nicee
Pieces.app
Excelent
Thanks 😀
Hello, thanks !
But one question, how will I know when it expires?
const token= jwt.sign({_id:this._id},process.env.JWTPRIVATEKEY,{ expiresIn:"EXPIRATIONTIME"});
Super useful, thanks!
I'm glad to hear it helped you 🙏🏾
Some comments may only be visible to logged-in visitors. Sign in to view all comments.