Select Page

The secure random token in Node.js

In this question Erik must generate a secure random token in Node.js. There is the method crypto.randomBytesthat generates a random Buffer. However, the base64 encoding in the node is not url-safe, includes /and +instead of -_. Therefore, the easiest way to generate this token I have found is


In-case synchronous option if you’re not a JS expert like me. I had to spend some time on how to access the inline function variable

var token = crypto.randomBytes(64).toString('hex');

WhatsApp chat