Skip to content

Instantly share code, notes, and snippets.

@odeke-em
Last active April 26, 2016 08:43
Show Gist options
  • Save odeke-em/2baefc47eeb4af49332e to your computer and use it in GitHub Desktop.
Save odeke-em/2baefc47eeb4af49332e to your computer and use it in GitHub Desktop.
const crypto = require('crypto');
function hexSha256ify(password) {
var sha256sum = crypto.createHash('sha256');
sha256sum.update(password + '');
return sha256sum.digest('hex');
}
function createNewUser(data) {
data = data || {};
return {
username: data.username || crypto.randomBytes(8) + '',
password: hexSha256ify(data.password || crypto.randomBytes(12)),
};
}
function login(data, callback) {
if (!(data && typeof data.username === 'string' && typeof data.password === 'string'))
return callback(400, 'expecting "username" and "password"'); // TODO: Check for password length
db.check(data.username, hexSha256ify(data.password), function(err, user) {
if (err)
return callback(500, err);
if (!user)
return callback(403, 'invalid login');
callback(200, user);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment