Advertisement
Guest User

usermodel

a guest
Apr 11th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. var
  4.   Sequelize = require('sequelize'),
  5.   sequelize = require('../database/database-connection').getInstance(),
  6.   bcrypt    = require('bcrypt'),
  7.   User = sequelize.define('User', {
  8.     username : Sequelize.STRING(30),
  9.     password : Sequelize.STRING,
  10.     name : Sequelize.STRING(50),
  11.     email : Sequelize.STRING,
  12.     token : Sequelize.STRING
  13.   }, {
  14.     timestamps : true,
  15.     tableName : 'User',
  16.     hooks : {
  17.       beforeCreate : (user, options, cb) => {
  18.         bcrypt.hash(user.password, 10, (err, hash) => {
  19.           user.password = hash;
  20.  
  21.           return cb(null, options);
  22.         });
  23.       },
  24.       beforeUpdate : (user, options, cb) => {
  25.         bcrypt.hash(user.password, 10, (err, hash) => {
  26.           user.password = hash;
  27.  
  28.           return cb(null, options);
  29.         });
  30.       }
  31.     },
  32.     instanceMethods : {
  33.       comparePassword : function comparePassword (candidatePassword, cb) {
  34.         bcrypt.compare(candidatePassword, this.getDataValue('password'), (err, isMatch) => {
  35.           if (err) {
  36.             cb(err);
  37.           } else {
  38.             cb(null, isMatch);
  39.           }
  40.         });
  41.       }
  42.     }
  43.   });
  44.  
  45. module.exports = User;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement