在此代码中,第一个函数是将查找数据库中的电子邮件地址和以哈希形式存在的密码。因此,比较哈希和密码。Findaccount()CompareHashAndPassword()
现在在文件中,我有一个名为的函数,它将允许用户登录。我在这里有一个问题。我调用了函数,但它只是验证一个电子邮件地址,没有验证正确的密码,并给我消息。handler.gologinData()database.Findaccount(email, password, hash)false
但是,如果我像这样调用该函数,它会验证电子邮件和密码。database.Findaccount(email, "1234", hash)
如何解决这个问题,因为我将无法记住每个密码。
数据库
func Findaccount(myEmail, myPassword, hash string) bool {
collection := Connect.Database("WebApp2").Collection("dataStored")
if err := collection.FindOne(context.TODO(), bson.M{"email": myEmail}).Decode(&Account); err != nil {
fmt.Println("Enter the correct email or password")
}
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(myPassword))
return err == nil
}
处理程序.go
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
return string(bytes), err
}
func loginData(w http.ResponseWriter, r *http.Request) {
email := r.FormValue("email")
password := r.FormValue("password")
hash, _ := HashPassword(password)
match := database.Findaccount(email, password, hash) // here is a problem
if match == false {
fmt.Println("false")
} else {
fmt.Println("true")
}
}
拉莫斯之舞
相关分类