簡體   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 :)

這是注冊表單,我得到哈希密碼:

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;
      }
    });
  }}>

這是登錄表單,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;

} 別的 {

        console.log('compare success')

} 如果(用戶){

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

    });
  }}

我嘗試了比較,比較同步,等待,即使這樣,但沒有任何效果,它不會識別散列密碼:

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

});

試試這個登錄。 請記住放棄一些飼料。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM