Guest User

Untitled

a guest
Sep 7th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. const bcrypt = require("bcrypt");
  2.  
  3. module.exports = (sequelize, DataTypes) => {
  4. const User = sequelize.define('User', {
  5. firstName: {
  6. type: DataTypes.STRING,
  7. validate: {
  8. len: {
  9. args: [1, 50],
  10. msg: "Please provide 'firstName' field within 1 to 50 characters."
  11. }
  12. }
  13. },
  14. email: {
  15. type: DataTypes.STRING,
  16. allowNull: false,
  17. unique: {
  18. args: true,
  19. msg: "This Email has been already taken"
  20. },
  21. validate: {
  22. isEmail: {
  23. msg: "Email must be Email"
  24. },
  25. notEmpty: {
  26. msg: "Email couldn't be empty"
  27. },
  28. unique(value, next) {
  29. User.find({
  30. where: { email: value }
  31. }).done((user) => {
  32. if (user) return next('This Email has been already taken');
  33. next();
  34. });
  35. }
  36. }
  37. },
  38. password: {
  39. type: DataTypes.STRING,
  40. validate: {
  41. notEmpty: {
  42. msg: "Password couldn't be empty"
  43. }
  44. }
  45. },
  46. isAdmin: {
  47. type: DataTypes.BOOLEAN,
  48. allowNull: false,
  49. defaultValue: false,
  50. validate: {
  51. boolean(val, next) {
  52. if (typeof val !== "boolean") return next("NOT BOOLEAN");
  53. next();
  54. }
  55. }
  56. }
  57. });
  58.  
  59. const generateHash = (password) => {
  60. return bcrypt.hash(password, 10);
  61. };
  62.  
  63. User.beforeCreate((user, options) => {
  64. user.email = user.email.toLowerCase();
  65. return generateHash(user.password).then(hashedPw => {
  66. user.password = hashedPw;
  67. });
  68. });
  69.  
  70. return User;
  71. };
Add Comment
Please, Sign In to add comment