Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bcrypt from 'bcrypt-nodejs';
- module.exports = (sequelize, DataTypes) => {
- const User = sequelize.define('User', {
- name: {
- type: DataTypes.STRING,
- allowNull: {
- args: false,
- msg: 'Please enter your name'
- }
- },
- username: {
- type: DataTypes.STRING,
- allowNull: {
- args: false,
- msg: 'Please enter username'
- },
- unique: {
- args: true,
- msg: 'Email already exists'
- },
- },
- email: {
- type: DataTypes.STRING,
- allowNull: {
- args: false,
- msg: 'Please enter your email'
- },
- validate: {
- isEmail: {
- args: true,
- msg: 'Please enter a valid email address'
- },
- },
- },
- password: {
- type: DataTypes.STRING,
- allowNull: {
- args: false,
- msg: 'Please enter password'
- },
- validate: {
- isNotStrong: (value) => {
- let strongRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{6,})");
- if (!strongRegex.test(value)) {
- 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');
- }
- },
- },
- },
- type: {
- type: DataTypes.STRING,
- allowNull: {
- args: true,
- msg: 'Type must be submitted'
- }
- },
- avatar: {
- type: DataTypes.STRING,
- allowNull: {
- args: true
- }
- },
- address: {
- type: DataTypes.STRING,
- allowNull: {
- args: true
- }
- },
- country_code: {
- type: DataTypes.STRING,
- allowNull: {
- args: true
- }
- },
- phone: {
- type: DataTypes.STRING,
- allowNull: {
- args: true
- }
- },
- google_2fa_secret: {
- type: DataTypes.STRING,
- allowNull: {
- args: true
- }
- },
- authy_id: {
- type: DataTypes.STRING,
- allowNull: {
- args: true
- }
- },
- phone_verified: {
- type: DataTypes.BOOLEAN,
- allowNull: {
- args: true
- },
- defaultValue: false
- },
- email_verified: {
- type: DataTypes.BOOLEAN,
- allowNull: {
- args: true
- },
- defaultValue: false
- },
- id_verified: {
- type: DataTypes.BOOLEAN,
- allowNull: {
- args: true
- },
- defaultValue: false
- },
- approved: {
- type: DataTypes.BOOLEAN,
- allowNull: {
- args: true
- },
- defaultValue: false
- },
- pending: {
- type: DataTypes.BOOLEAN,
- allowNull: {
- args: true
- },
- defaultValue: true
- },
- disapproved: {
- type: DataTypes.BOOLEAN,
- defaultValue: false,
- allowNull: {
- args: true
- }
- }
- }, {});
- User.beforeSave((user, options) => {
- if (user.changed('password')) {
- user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null);
- }
- });
- User.prototype.comparePassword = function (password, callback) {
- bcrypt.compare(password, this.password, function (error, isMatch) {
- if (error) {
- return callback(error);
- }
- callback(null, isMatch);
- });
- };
- User.associate = (models) => {
- // associations can be defined here
- };
- return User;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement