Last active
April 26, 2016 08:43
-
-
Save odeke-em/2baefc47eeb4af49332e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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