Skip to content

Instantly share code, notes, and snippets.

@coderberry
Created July 29, 2014 14:21
Show Gist options
  • Select an option

  • Save coderberry/b71050fb0a84c8a82292 to your computer and use it in GitHub Desktop.

Select an option

Save coderberry/b71050fb0a84c8a82292 to your computer and use it in GitHub Desktop.
/**
* User.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/#!documentation/models
*/
"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();
});
});
}
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' }
},
/**
* Before create callback.
*
* @param {sails.model.user} values
* @param {Function} next
*/
beforeCreate: function(values, next) {
hashPassword(values, next);
},
};
module.exports = User;
/**
* UserController
*
* @description :: Server-side logic for managing users
* @help :: See http://links.sailsjs.org/docs/controllers
*/
module.exports = {
'new': function (req, res) {
res.view('user/new');
},
create: function (req, res, next) {
var userParams = {
firstName: req.param('firstName'),
lastName: req.param('lastName'),
email: req.param('email'),
password: req.param('password'),
passwordConfirmation: req.param('passwordConfirmation'),
role: 0
}
User.create(userParams, function(err, user) {
// ...
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment