isPasswordValid始终返回false

时间:2018-08-12 08:51:46

标签: javascript node.js express sequelize.js bcrypt

我正在尝试将登录名添加到我的快速代码中,但似乎始终是isPassword = false,但电子邮件为true,所以我无法登录,我仍然困惑于使用bycript-nodejs。我认为它受节点

支持

这是我的控制人

 const { response } = require('../helpers/index');
    const { users: User } = require('../models/index');
    const axios = require('axios');
    const config = require('../config/config');
    const jwtS = require('../config/configJwt')
    const jwt = require('jsonwebtoken')
    const Sequelize = require('sequelize');
    const { Op } = Sequelize;

    const jwtSignUser = (user) => {
        const ONE_WEEK = 60 * 60 * 24 * 7
        return jwt.sign(user, jwtS.authentication.jwtSecret, {
            expiresIn: ONE_WEEK
        })
    }    
module.exports = {
        async register(req, res) {
            try {
                const user = await User.create(req.body)
                const userJson = user.toJSON()
                res.send({
                    user: userJson,
                    token: jwtSignUser(userJson)
                })
            }
            catch (err) {
                res.status(400).send({
                    error: err

                })
            }
        },

        async login(req, res) {
            try {
                const { email, password } = req.body
                const user = await User.findOne({
                    where: {
                        email: email
                    }
                })

                if (!user) {
                    return res.status(403).send({
                        error: err + 'The login user information was incorrect'
                    })
                }
                const isPasswordValid = await user.comparePassword(password)
                if (!isPasswordValid) {
                    console.log(isPasswordValid)
                    return res.status(403).send({
                        error: 'The login password information was incorrect'
                    })
                }

                const userJson = user.toJSON()
                res.send({
                    user: userJson,
                    token: jwtSignUser(userJson)
                })
            } catch (err) {
                res.status(500).send({
                    error: 'An error has occured trying to log in'
                })
            }

            } 
    }

这是我的数据库模型,我认为数据库是正确的,不需要在数据库中进行解析:

 'use strict';
    const Promise = require('bluebird')
    const bcrypt = Promise.promisifyAll(require("bcrypt-nodejs"))

    function hashPassword(user, options) {
      const SALT_FACTOR = 8

      if (!user.changed("password")) {
        return
      }

      return bcrypt
        .genSaltAsync(SALT_FACTOR)
        .then(salt => bcrypt.hashSync(user.password, salt, null))
        .then(hash => {
          user.setDataValue('password', hash)
        })
    }
    module.exports = (sequelize, DataTypes) => {
      const User = sequelize.define('users', {
        full_name: {
          type: DataTypes.STRING,
          allowNull: false,
        },
        email: {
          type: DataTypes.STRING,
          allowNull: false,
          unique : true,
        },
        password: {
          type: DataTypes.STRING,
          allowNull: false,
        },
        phone: {
          type: DataTypes.STRING,
          allowNull: false,
        },
      }, {  
      hooks :{
        beforeCreate: hashPassword,
        beforeUpdate: hashPassword,
        beforeSave:hashPassword
      }
      });

      User.prototype.comparePassword = function(password){
        return bcrypt.compareSync(password, this.password)
      }

      User.associate = (models) => {
        User.hasMany(models.authors, {
          foreignKey: 'user_id',
          onDelete: 'CASCADE'
        });
      };

      return User;
    };

你们能帮助我解决我的问题,非常感谢您的帮助?

0 个答案:

没有答案