Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- var
- Sequelize = require('sequelize'),
- sequelize = require('../database/database-connection').getInstance(),
- bcrypt = require('bcrypt'),
- User = sequelize.define('User', {
- username : Sequelize.STRING(30),
- password : Sequelize.STRING,
- name : Sequelize.STRING(50),
- email : Sequelize.STRING,
- token : Sequelize.STRING
- }, {
- timestamps : true,
- tableName : 'User',
- hooks : {
- beforeCreate : (user, options, cb) => {
- bcrypt.hash(user.password, 10, (err, hash) => {
- user.password = hash;
- return cb(null, options);
- });
- },
- beforeUpdate : (user, options, cb) => {
- bcrypt.hash(user.password, 10, (err, hash) => {
- user.password = hash;
- return cb(null, options);
- });
- }
- },
- instanceMethods : {
- comparePassword : function comparePassword (candidatePassword, cb) {
- bcrypt.compare(candidatePassword, this.getDataValue('password'), (err, isMatch) => {
- if (err) {
- cb(err);
- } else {
- cb(null, isMatch);
- }
- });
- }
- }
- });
- module.exports = User;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement