Advertisement
Guest User

Untitled

a guest
Dec 20th, 2018
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. const bcrypt = require('bcrypt');
  4.  
  5. const SALT_ROUNDS = 10;
  6.  
  7. function hashPassword(user) {
  8.   if (user.changed('password')) {
  9.     return bcrypt.hash(user.password, SALT_ROUNDS)
  10.       .then((hashedPass) => { user.password = hashedPass; });
  11.   }
  12. }
  13.  
  14. module.exports = (sequelize, DataTypes) => {
  15.   const User = sequelize.define('User', {
  16.     username: { type: DataTypes.STRING, allowNull: false, unique: true },
  17.     password: { type: DataTypes.STRING, allowNull: false },
  18.   }, {
  19.     hooks: {
  20.       beforeCreate: hashPassword,
  21.       beforeUpdate: hashPassword,
  22.     },
  23.     classMethods: {
  24.       associate() {
  25.         // associations can be defined here
  26.       },
  27.     },
  28.     instanceMethods: {
  29.       isValidPassword(password) {
  30.         return bcrypt.compare(password, this.password);
  31.       },
  32.     },
  33.   });
  34.  
  35.   return User;
  36. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement