Advertisement
Guest User

Untitled

a guest
Apr 11th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import bcrypt from 'bcrypt-nodejs';
  2.  
  3. module.exports = (sequelize, DataTypes) => {
  4.     const User = sequelize.define('User', {
  5.         name: {
  6.             type: DataTypes.STRING,
  7.             allowNull: {
  8.                 args: false,
  9.                 msg: 'Please enter your name'
  10.             }
  11.         },
  12.         username: {
  13.             type: DataTypes.STRING,
  14.             allowNull: {
  15.                 args: false,
  16.                 msg: 'Please enter username'
  17.             },
  18.             unique: {
  19.                 args: true,
  20.                 msg: 'Email already exists'
  21.             },
  22.         },
  23.         email: {
  24.             type: DataTypes.STRING,
  25.             allowNull: {
  26.                 args: false,
  27.                 msg: 'Please enter your email'
  28.             },
  29.             validate: {
  30.                 isEmail: {
  31.                     args: true,
  32.                     msg: 'Please enter a valid email address'
  33.                 },
  34.             },
  35.         },
  36.         password: {
  37.             type: DataTypes.STRING,
  38.             allowNull: {
  39.                 args: false,
  40.                 msg: 'Please enter password'
  41.             },
  42.             validate: {
  43.                 isNotStrong: (value) => {
  44.                     let strongRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{6,})");
  45.                     if (!strongRegex.test(value)) {
  46.                         throw new Error('Password should be at least 8 characters, must contain at least 1 lowercase alphabetical character, 1 uppercase alphabetical character, 1 numeric character, one special character');
  47.                     }
  48.                 },
  49.             },
  50.         },
  51.         type: {
  52.             type: DataTypes.STRING,
  53.             allowNull: {
  54.                 args: true,
  55.                 msg: 'Type must be submitted'
  56.             }
  57.         },
  58.         avatar: {
  59.             type: DataTypes.STRING,
  60.             allowNull: {
  61.                 args: true
  62.             }
  63.         },
  64.         address: {
  65.             type: DataTypes.STRING,
  66.             allowNull: {
  67.                 args: true
  68.             }
  69.         },
  70.         country_code: {
  71.             type: DataTypes.STRING,
  72.             allowNull: {
  73.                 args: true
  74.             }
  75.         },
  76.         phone: {
  77.             type: DataTypes.STRING,
  78.             allowNull: {
  79.                 args: true
  80.             }
  81.         },
  82.         google_2fa_secret: {
  83.             type: DataTypes.STRING,
  84.             allowNull: {
  85.                 args: true
  86.             }
  87.         },
  88.         authy_id: {
  89.             type: DataTypes.STRING,
  90.             allowNull: {
  91.                 args: true
  92.             }
  93.         },
  94.         phone_verified: {
  95.             type: DataTypes.BOOLEAN,
  96.             allowNull: {
  97.                 args: true
  98.             },
  99.             defaultValue: false
  100.         },
  101.         email_verified: {
  102.             type: DataTypes.BOOLEAN,
  103.             allowNull: {
  104.                 args: true
  105.             },
  106.             defaultValue: false
  107.         },
  108.         id_verified: {
  109.             type: DataTypes.BOOLEAN,
  110.             allowNull: {
  111.                 args: true
  112.             },
  113.             defaultValue: false
  114.         },
  115.         approved: {
  116.             type: DataTypes.BOOLEAN,
  117.             allowNull: {
  118.                 args: true
  119.             },
  120.             defaultValue: false
  121.         },
  122.         pending: {
  123.             type: DataTypes.BOOLEAN,
  124.             allowNull: {
  125.                 args: true
  126.             },
  127.             defaultValue: true
  128.         },
  129.         disapproved: {
  130.             type: DataTypes.BOOLEAN,
  131.             defaultValue: false,
  132.             allowNull: {
  133.                 args: true
  134.             }
  135.         }
  136.     }, {});
  137.  
  138.     User.beforeSave((user, options) => {
  139.         if (user.changed('password')) {
  140.             user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null);
  141.         }
  142.     });
  143.  
  144.  
  145.     User.prototype.comparePassword = function (password, callback) {
  146.         bcrypt.compare(password, this.password, function (error, isMatch) {
  147.             if (error) {
  148.                 return callback(error);
  149.             }
  150.             callback(null, isMatch);
  151.         });
  152.     };
  153.  
  154.  
  155.     User.associate = (models) => {
  156.         // associations can be defined here
  157.     };
  158.  
  159.  
  160.     return User;
  161. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement