Last active
August 29, 2015 14:04
-
-
Save japel/15a453007f90b350a731 to your computer and use it in GitHub Desktop.
This file contains 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
"use strict"; | |
var bcrypt = require('bcrypt'); | |
function hashPassword(attrs, next) { | |
bcrypt.genSalt(10, function(err, salt) { | |
if (err) return next(err); | |
bcrypt.hash(attrs.password, salt, function(err, hash) { | |
if (err) return next(err); | |
attrs.encryptedPassword = hash; | |
next(); | |
}); | |
}); | |
} | |
var User = { | |
schema: true, | |
attributes: { | |
firstName: { type: 'string', required: true }, | |
lastName: { type: 'string', required: true }, | |
email: { type: 'string', unique: true, required: true }, | |
encryptedPassword: { type: 'string', required: true }, | |
role: { type: 'integer', required: true } | |
}, | |
beforeCreate: function(values, next) { | |
hashPassword(values, next); | |
}, | |
}; | |
module.exports = User; |
This file contains 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
module.exports = { | |
create: function (req, res, next) { | |
var params = req.params.all(); | |
User.create(params, function(err, user){ | |
if(err) return next(err); | |
res.status(201); | |
res.json(user); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment