issue #391 fixed

parent 148cb19d
...@@ -151,20 +151,25 @@ module.exports = { ...@@ -151,20 +151,25 @@ module.exports = {
// adds a new supervisor into the database // adds a new supervisor into the database
// //
create: function(req, res) { create: function(req, res) {
var params = req.params.all(); var params = req.params.all();
delete params.email_confirm;
delete params.password_confirm;
Supervisor.create(params).exec(function (err, created) { Supervisor.create(params).exec(function (err, created) {
if (err) { if (err) {
console.log(err); console.log(err);
return res.json(500, {error: 'User not created'}); return res.json(500, {error: 'User not created'});
} }
if (created) { if (created) {
// Send email confirmation // TODO: Send email confirmation
console.log("sending mail to user" ); // created.sendMail();
created.sendMail(); return res.json({
return res.json({user: created, token: sailsTokenAuth.issueToken(created.id)}); user: created,
} token: sailsTokenAuth.issueToken(created.id)});
}); } else {
return res.json(500, {error: 'User not created'});
}
});
}, },
// //
......
...@@ -186,32 +186,33 @@ module.exports = { ...@@ -186,32 +186,33 @@ module.exports = {
// //
beforeCreate: function(attrs, next) { beforeCreate: function(attrs, next) {
// // We have to encryption AFTER checking user does not exist
// Check that user does not exist // so... we use async.series to ensure task completion
// NOTE: This is not needed as uniqueness is granted by DB
//
Supervisor.findByEmail(attrs.email).exec(function(err, users) {
if (err)
return next(err);
if (users.length > 0)
return next(new Error('User exists'));
});
//
// Encrypt password before insertion
//
var bcrypt = require('bcrypt-nodejs');
bcrypt.genSalt(10, function(err, salt) { async.series([
if (err) function(cb) { // check email is new
return next(err); Supervisor.findByEmail(attrs.email).exec(function(err, users) {
bcrypt.hash(attrs.password, salt, null, function(err, hash) { if (err) return cb(err);
if (err) if (users.length > 0)
return next(err); return cb(new Error('User exists'));
attrs.password = hash; cb();
next(); });
}); },
}); function(cb) { // encrypt password
var bcrypt = require('bcrypt-nodejs');
bcrypt.genSalt(10, function(err, salt) {
if (err) return cb(err);
bcrypt.hash(attrs.password, salt, null, function(err, hash) {
if (err) return cb(err);
attrs.password = hash;
cb();
});
});
}
], function(err) {
if (err) return next(err);
next();
});
}, },
// //
...@@ -220,37 +221,45 @@ module.exports = { ...@@ -220,37 +221,45 @@ module.exports = {
// //
beforeUpdate: function(attrs, next) { beforeUpdate: function(attrs, next) {
// async.series([
// Check that user email does not exist function(cb) {
// //
if(attrs.email){ // Check that user email does not exist
Supervisor.findByEmail(attrs.email).exec(function(err, users) { //
if (err) if(attrs.email){
return next(err); Supervisor.findByEmail(attrs.email).exec(function(err, users) {
if (users.length > 0) if (err)
return next(new Error('User email already exists')); return cb(err);
}); if (users.length > 0)
} return cb(new Error('User email already exists'));
cb();
// });
// Encrypt password before insertion } else {
// return cb(new Error('No email in user attributes'));
if(attrs.password){ }
var bcrypt = require('bcrypt-nodejs'); },
function(cb) {
bcrypt.genSalt(10, function(err, salt) { //
if (err) // Encrypt password before insertion
return next(err); //
bcrypt.hash(attrs.password, salt, null, function(err, hash) { if(attrs.password){
if (err) var bcrypt = require('bcrypt-nodejs');
return next(err);
attrs.password = hash; bcrypt.genSalt(10, function(err, salt) {
next(); if (err) return cb(err);
}); bcrypt.hash(attrs.password, salt, null, function(err, hash) {
}); if (err) return cb(err);
}else{ attrs.password = hash;
next(); cb();
} });
});
}else{
cb();
}
}], function(err) {
if (err) return next(err);
next();
});
}, },
// //
......
...@@ -21,7 +21,7 @@ dashboardControllers.controller('SignInCtrl', function SignInCtrl($scope, $http, ...@@ -21,7 +21,7 @@ dashboardControllers.controller('SignInCtrl', function SignInCtrl($scope, $http,
lang: 'es' lang: 'es'
}; };
reCAPTCHA.setPublicKey('6LdkZwMTAAAAANDR_7_y9_ifEve1gLPcgneM_50o'); reCAPTCHA.setPublicKey('6LdLjh0TAAAAANblo_KUGNnmRZuIetOkdjdhj1b6');
/* NOT NECESSARY /* NOT NECESSARY
// Array of key terms to translate // Array of key terms to translate
...@@ -84,8 +84,8 @@ dashboardControllers.controller('SignInCtrl', function SignInCtrl($scope, $http, ...@@ -84,8 +84,8 @@ dashboardControllers.controller('SignInCtrl', function SignInCtrl($scope, $http,
$scope.alert = "alert-danger"; $scope.alert = "alert-danger";
$scope.message = "user_exists"; $scope.message = "user_exists";
console.log("Error from API: " + data.error); console.log("Error from API: " + status);
}); });
}; };
}); });
\ No newline at end of file
...@@ -102,4 +102,4 @@ ...@@ -102,4 +102,4 @@
</div> </div>
<!-- Fin de container --> <!-- Fin de container -->
<footer-translate></footer-translate> <footer-translate></footer-translate>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment