Guest User

Untitled

a guest
Oct 24th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. // api/models
  2. import bcrypt from 'bcrypt';
  3.  
  4. module.exports = {
  5. attributes: {
  6. username: {
  7. type: 'string',
  8. required: true,
  9. unique: true,
  10. },
  11.  
  12. email: {
  13. type: 'string',
  14. required: true,
  15. unique: true,
  16. isEmail: true,
  17. },
  18.  
  19. password: {
  20. type: 'string',
  21. required: true,
  22. },
  23. },
  24.  
  25. customToJSON: function() {
  26. return _.omit(this, ['password']);
  27. },
  28.  
  29. // Lifecycle
  30. beforeCreate: function(values, next) {
  31. if (values.password) {
  32. bcrypt.genSalt(10, function (err, salt) {
  33. if (err) return next(err);
  34. bcrypt.hash(values.password, salt, function (err, hash) {
  35. if (err) return next(err);
  36. values.password = hash;
  37. next();
  38. });
  39. });
  40. } else {
  41. next();
  42. }
  43. },
  44. };
Add Comment
Please, Sign In to add comment