Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 检查密码是否与指定的用户对象密码匹配_Node.js_Mongodb - Fatal编程技术网

Node.js 检查密码是否与指定的用户对象密码匹配

Node.js 检查密码是否与指定的用户对象密码匹配,node.js,mongodb,Node.js,Mongodb,您好,我面临的问题我有一个功能,其中传递电子邮件和密码,然后使用findByemail在mongodb中找到特定用户,findByemail获取与提供的电子邮件对应的用户对象,如果用户不存在或密码与包含的用户对象不匹配,则密码应抛出api错误 /** * Login with username and password * - Utilize userService methods to fetch user object corresponding to the email provide

您好,我面临的问题我有一个功能,其中传递电子邮件和密码,然后使用findByemail在mongodb中找到特定用户,findByemail获取与提供的电子邮件对应的用户对象,如果用户不存在或密码与包含的用户对象不匹配,则密码应抛出api错误

/**
 * Login with username and password
 * - Utilize userService methods to fetch user object corresponding to the email provided
 * - If user doesn't exist or incorrect password,
 * throw an ApiError with "401 Unauthorized" status code and message, "Incorrect email or password"
 * - Else, return the user object

const loginUserWithEmailAndPassword = async (email, password) => {
  const data = await userService.getUserByEmail(email);
  // console.log(data);
  if((!data)&&(User.isPasswordMatch(password)))
  {
    throw  new ApiError(httpStatus.UNAUTHORIZED,"Incorrect email or password")
  }
  return data;

  };
我已经创建了一个比较密码的函数,但是在使用它的用户模型文件中,我完成了密码不正确的任务

userSchema.methods.isPasswordMatch = async function (password) {
  const user = this;
  return bcrypt.compare(password, user.password);
};
const User=mongoose.model('User',userSchema)


错误是当运行npm测试时,我得到的isPasswordMatch不是一个函数,我知道我错误地实现了它

您可以在用户模式中使用这样的回调

UserSchema.methods.comparePassword = function(candidatePassword, cb) {
    bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
        if (err) return cb(err);
        cb(null, isMatch);
    });
};

在mongoose数据库中注册密码时,密码以加密形式存储,但在登录时,您不使用密码的加密形式,而是使用密码本身,我们可以创建一个密码匹配方法,其中定义了userschema,然后在user.getUserbyemail()中已在userservices中定义的将用于检索对象,该对象的电子邮件与您登录时提供的电子邮件相同。如果电子邮件不存在,则user.getuerbyemail将返回错误,但如果存在电子邮件,则会收到数据,然后检查输入的密码是否与从中收到的密码匹配userservices.getuserbyemail User model.js(仅密码检查部分)

js(getUserByEmail部分)

主体部分

const loginUserWithEmailAndPassword = async (email, password) => {
  const data = await userService.getUserByEmail(email);
   // console.log(data);
  // console.log(data.password);
  if (!data || !(await data.isPasswordMatch(password))) {
    throw new ApiError(httpStatus.UNAUTHORIZED, "Incorrect email or password");
  }
  return data;

  };
 const getUserByEmail = async(email) =>{
    const data = await User.findOne({"email":email})
    
    return data;
 }
 
const loginUserWithEmailAndPassword = async (email, password) => {
  const data = await userService.getUserByEmail(email);
   // console.log(data);
  // console.log(data.password);
  if (!data || !(await data.isPasswordMatch(password))) {
    throw new ApiError(httpStatus.UNAUTHORIZED, "Incorrect email or password");
  }
  return data;

  };