Useful Node.js Identification Modules

When constructing an API server for a mobile app in Node.js there are several common identification and authentication tasks:

  • Generate a unique id or token for users
  • Encrypt passwords
  • Generate a random password (for password reset)
  • Generate a random SMS confirmation code (e.g. Viber)

To generate a unique id or token, use the UDID module.

var uuid = require('node-uuid');
// generate token
var token = uuid.v4();

To encrypt passwords, use the Bcrypt module.

var bcrypt = require('bcrypt');
var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync(
       req.param("password"), salt);

To generate a random password, use the password-generator module.

var generatePassword = require('password-generator');
var randomPassword = generatePassword(10);

To generate a 5 digit SMS confirmation code:

var generatePassword = require('password-generator');
var code = generatePassword(5, false, /\d/) ;