/**
* 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
*/

var bcrypt = require('bcrypt');
var assert = require('assert');

module.exports = {

  schema: true,

  attributes: {
    name: {
      type: "string",
      required: true
    },
    username: {
      type: "string",
      required: true,
      unique: true
    },
    email: {
      type: "string",
      email: true,
      required: true,
      unique: true
    },
    admin: {
      type: "boolean",
      defaultsTo: false
    },
    encryptedPassword: {
      type: "string",
    },

    toJSON: function() {
      var obj = this.toObject();
      delete obj.password;
      delete obj.confirmation;
      delete obj.encryptedPassword;
      delete obj._csrf;
      return obj;
    }
  },

  beforeValidation: function (values, callback) {

    if (typeof values.admin !== 'undefined') {
      if (values.admin === 'unchecked') {
        values.admin = false;
      } else if (values.admin[1] === 'on') {
        values.admin = true;
      }
    }

    callback();

  },

  beforeCreate: function (values, next) {
    if(!values.password || values.password != values.confirmation) {
      return next({err: ["Password doesn't match password confirmation."]});
    }

    require('bcrypt').hash(values.password, 10, function passwordEncrypted(err, encryptedPassword) {
      if(err)
        return next(err);
      values.encryptedPassword = encryptedPassword;
      next();
    });
  }

};

@@ -1198,22 +1198,28 @@
 es, 
-next
+callback
 ) %7B%0A
+%0A
     if
+ 
 (!va
@@ -1235,16 +1235,107 @@
 word %7C%7C 
+!values.confirmation) %7B%0A       return callback('Password required.', null);%0A    %7D else if (
 values.p
@@ -1385,29 +1385,26 @@
 urn 
-next(%7Berr: %5B%22
+callback('
 Password
  doe
@@ -1403,50 +1403,75 @@
 word
+s
  do
-esn'
+ no
 t match
- password confirmation.%22%5D%7D);
+.', null); // Return the error and a null user object
 %0A   
@@ -1478,33 +1478,22 @@
  %7D%0A%0A    
-require('
 bcrypt
-')
 .hash(va
@@ -1537,20 +1537,23 @@
 ncrypted
+ 
 (err
+or
 , encryp
@@ -1555,24 +1555,16 @@
 ncrypted
-Password
 ) %7B%0A    
@@ -1567,21 +1567,26 @@
       if
+ 
 (err
-)
+or) %7B
 %0A       
@@ -1597,50 +1597,257 @@
 urn 
-next(err);%0A      values.encryptedP
+callback(error, null); // Return the error and a null user object%0A      %7D else %7B%0A        try %7B%0A%0A          // Assert that the password is encrypted%0A          assert.notEqual(values.password, encrypted);%0A%0A          // Update the value of the p
 assword 
 = en
@@ -1842,17 +1842,21 @@
 assword 
-=
+to be
  encrypt
@@ -1861,31 +1861,286 @@
 pted
-Password;%0A      next();
+%0A          values.password = encrypted;%0A         %0A          // Return no error %0A          return callback(null, values);%0A%0A        %7D catch (error) %7B%0A%0A          // If an error is caught, return it and a null user object.%0A          return callback(error, null); %0A%0A        %7D%0A      %7D
 %0A   
@@ -2144,16 +2144,17 @@
     %7D);%0A
+%0A
   %7D%0A%0A%7D;%0A

Add try...catch & conditionals to `beforeCreate()`