DEV Community

Tonny Kirwa
Tonny Kirwa

Posted on

Generate a Random JWT Secret Key

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:

  1. Open your terminal or command prompt.
  2. Run the following Node.js script to generate a random string:
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
Enter fullscreen mode Exit fullscreen mode

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.

  1. Copy the generated string.

  2. Open your .env file and set the JWT secret key:

JWT_SECRET=paste-the-generated-string-here
Enter fullscreen mode Exit fullscreen mode

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 (6)

Collapse
 
kevdubinks profile image
kevdubinks

merci cela m'a beaucoup aidé

Collapse
 
tkirwa profile image
Tonny Kirwa

Bienvenue 😀 N'hésitez pas à le partager avec vos amis.

Collapse
 
climontectos profile image
Carlos Limón

Excelent

Collapse
 
tkirwa profile image
Tonny Kirwa

Thanks 😀

Collapse
 
sergio_jz profile image
Sergio J

Thanks!!!

Collapse
 
louis_pierrefrancoisdar profile image
Louis Pierre Francois Darell Musette

Hello, thanks !
But one question, how will I know when it expires?
const token= jwt.sign({_id:this._id},process.env.JWTPRIVATEKEY,{ expiresIn:"EXPIRATIONTIME"});