简体   繁体   English

我在使用 bcrypt 比较时遇到问题。 我在注册页面上成功输入了 hash 密码,但在登录页面上我没有成功比较。 请帮忙:)

[英]I am having a problem with bcrypt compare. I succed to hash password on Register page, but I not to compare successfully on login page. Please help :)

Here is Register form, I get the hashed password:这是注册表单,我得到哈希密码:

getAllUsers().then((res) => {
      let user = res.data.find(
        (u) => u.ugovor === ugovor && u.merilo === merilo
      );

      if (user) {
        setUser(user) {
 
        bcrypt.genSalt(10, (err, salt)=> {
          bcrypt.hash(loggedIn.password, salt, (err, hash) => {
            
            postUser(loggedIn.id, hash, password, email).then((res) => {
              user = res.data;
              setPassword(prev => [...prev, hash]);
              //loggedIn.password = hash;
              setEmail(prev => [...prev, user]);
              history("/login");
              setError("");
              console.log("uspesna registracija");
            })
          })
        }) 
      } else {
        setError("Не постоји купац са унесеним бројем Уговора и мерила");
        return 0;
      }
    });
  }}>

And here is LogIn form, everytnihg works but compare hashed password:这是登录表单,everytnihg 有效,但比较散列密码:

getAllUsers().then((res) => {
     
      let user = res.data.find(
        
        (u) => u.ugovor === ugovor && u.password === password
      );
       const doesPasswordMatch = bcrypt.compareSync(password, user.password); 
       if(!doesPasswordMatch) {
         console.log('compare not success');
         return 0;

} else { } 别的 {

        console.log('compare success')

} if (user) { } 如果(用户){

        setLogIn(true);
        setUser(user);
        history("/profile");
        console.log("uspesno ulogovan");
       
      } else {
        setError("Broj Ugovora i lozinka se ne podudaraju");
      }   

    });
  }}

I tried compare, compareSync, with await, even this, but nothing works, it wont recognise hashed password:我尝试了比较,比较同步,等待,即使这样,但没有任何效果,它不会识别散列密码:

bcrypt.compare(password, user.password, function(err, result) {
// result == true

}); });

Try this for your login.试试这个登录。 Please, remember to give some feed up.请记住放弃一些饲料。

getAllUsers().then((res) => {
 // look for user
 let user = res.data.find((u) => u.ugovor === ugovor);

 if (user) {
  // user is found
  const hashedPassword = u.password; // get stored hashed password
  // compare passwords
  const doesPasswordMatch = bcrypt.compareSync(password, hashedPassword, (err, result) => {
    if (err) console.log(err);
     // result == true
    });

  if (doesPasswordMatch) {
   // correct password
   console.log("compare success");
   setLogIn(true);
   setUser(user);
   history("/profile");
   console.log("uspesno ulogovan");
  } else {
   // wrong password
   console.log("compare not success");
   return 0;
  }
} else {
 // user is not found
 setError("Broj Ugovora i lozinka se ne podudaraju");
}

}); });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较 bcrypt hash 密码 - how to compare bcrypt hash password 为什么即使两个密码字符串完全匹配,我也会从 bcrypt 比较中得到 isMatch null? - Why am I getting isMatch null from bcrypt compare even though two password string matches perfectly? 我正在使用 bcrypt 创建登录 api 和散列密码 - i am creating login api and hashed password using bcrypt Nodejs:在bcrypt中,它在比较密码哈希时返回false - Nodejs : in bcrypt it returns false at compare password hash 用户成功注册并将其重定向到“登录”页面后,如何显示消息框? - How can I show messagebox when user register successfully and redirect him to Login page? 使用 ReactJS 和 ChakraUI 构建登录页面。 尝试使用图像,但它不会出现在页面上。 我究竟做错了什么? - Building a Login Page using ReactJS and ChakraUI. Trying to use an image but it will not appear on the page. What am I doing wrong? 我在html索引页面中有2个div。 在每个div内,我需要从js文件调用一个函数 - I am having 2 divs in html index page. Inside each div i need to call one function from js file 我无法在一页上获取多个同种型图。 我认为问题可能出在 svg<use> 元素 .selectAll(&quot;使用&quot;) - I'm having trouble getting multiple isotype graphs on one page. I think the problem may be with the svg <use> element .selectAll("use") 无法将密码与 bcrypt compare 进行比较 - cannot compare password with bcrypt compare 在 Github 上部署我的 React 应用程序后,我收到 404 错误页面(请帮忙) - I am getting a 404 error page after deploying my react app on Github (help please)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM