Javascript 无效的凭据Bcyrpt.compare()

Javascript 无效的凭据Bcyrpt.compare(),javascript,node.js,mongoose,bcrypt,Javascript,Node.js,Mongoose,Bcrypt,在我的数据库中,我有电子邮件和密码 在我的邮递员中,当我向https://localhost:5000/api/auth 该电子邮件是正确的,但我不断获得的密码是不正确的,与此常量isMatch=wait bcrypt.compare(密码,user.password) 我控制台同时记录了password和user.password,它的值是相同的,我不明白为什么!isMatch不断被触发 有人能帮我吗bcrypt.compare()的语法是: bcrypt.compare(明文密码、哈希) 第

在我的数据库中,我有电子邮件和密码 在我的邮递员中,当我向https://localhost:5000/api/auth

该电子邮件是正确的,但我不断获得的密码是不正确的,与此常量isMatch=wait bcrypt.compare(密码,user.password)

我控制台同时记录了password和user.password,它的值是相同的,我不明白为什么!isMatch不断被触发


有人能帮我吗bcrypt.compare()的语法是:

bcrypt.compare(明文密码、哈希)


第一个参数是明文密码,第二个参数是真实密码的哈希值,因此它将不是相同的值。正如您提到的,您的
密码
用户.密码
是相同的值,我猜您在将用户密码保存到数据库之前忘记对其进行哈希运算。请查看以了解更多详细信息。

当您说“我同时记录了password和user.password,并且它的值相同”时,您的意思是什么。你在哪里记录的?
    const express = require('express');
const router = express.Router();
const auth = require('../../middleware/auth');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const config = require('config');
const { check, validationResult } = require('express-validator');
 
 
const User = require('../../models/User');
// @route Get api/auth
// @desc Test route
// @access Public
router.get('/', auth, async (req, res) => {
 
 
  try {
    const user = await User.findById(req.user.id).select('-password');
    res.json(user);
  } catch(err) {
    console.error(err.message);
    res.status(500).send('Server Error')
  }
});
 
 
// @route POST api/auth
// @desc Authenticate User And Get Token
// @access Public
router.post('/',
[
  check('email', 'Please include a valid email').isEmail(),
  check('password', 'Password is required').exists()
],
 
async (req, res) => {
 
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(400).json({ errors: errors.array()});
  }
 
  const { email, password } = req.body;
 
  try {
    // See if user exists
      let user = await User.findOne({ email})
 
      if (!user) {
        return res
        .status(400)
        .json({ errors: [ { msg: 'Invalid Credentials Email' } ] });
      }
 
 
    // Make Sure Password matches
      const isMatch = await bcrypt.compare(password, user.password);
 
      if(!isMatch) {
        return res
        .status(400)
        .json({ errors: [ { msg: 'Invalid Credentials Password' } ] });
      }
 
 
 
      const payload = {
        user: {
          id: user.id
        }
      }
 
      jwt.sign(
        payload,
        config.get('jwtSecret'),
        { expiresIn: 360000 },
        (err, token) => {
          if(err) throw err;
 
          res.json({ token });
        }
        );
  } catch(err) {
    console.error(err.message);
    res.status(500).send('Server error')
  }
 
 
});
 
module.exports = router
  if(!isMatch) {
    return res
    .status(400)
    .json({ errors: [ { msg: 'Invalid Credentials Password' } ] });
  }