我正在开发用于两步验证的 QR 生成器应用程序。虽然 main.go 和 users.go 中的登录凭据正确,但登录验证仍然失败。这是代码:
在main.go
func loginHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
_ = templates.ExecuteTemplate(w, "login.html", nil)
return
}
r.ParseForm()
username := strings.TrimSpace(r.Form.Get("username"))
password := strings.TrimSpace(r.Form.Get("password"))
user, ok := users[username]
if !ok || user.Password != password {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
if user.Secret == "" {
http.Redirect(w, r, "/generate-otp?username="+username, http.StatusFound)
return
}
_ = templates.ExecuteTemplate(w, "validate.html", struct{ Username string }{Username: username})
}
用户.go
package users
// User represents a user with a username, password, and a TOTP secret.
type User struct {
Username string
Password string
Secret string
}
// In-memory "database" of users
var Users = map[string]*User{
"john": {Username: "john", Password: "123" , Secret: ""},
}
我已经检查了用户名和密码,但仍然显示身份验证错误。
你发布了完整的代码吗?您似乎创建了地图“Users”,但您试图在 main.go 中使用小写“users”访问该地图。也许我错过了什么?