【问题标题】:authentication on golang/MySQL doesn't workgolang/MySQL 上的身份验证不起作用
【发布时间】:2021-04-18 17:08:06
【问题描述】:

我想在网站上创建一个注册/授权,注册已经在工作,但由于某种原因验证码没有。如果输入的数据正确,我希望转到主页(并且消息显示在相反的情况下),但它没有去。也许我做错了一般的事情,也许问题出在代码中。这是代码本身:

func log(w http.ResponseWriter, r *http.Request) {
FormEmail := r.FormValue("email")
FormPassword := r.FormValue("password")
var email string
var password string
// подключение
db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:8889)/service")
if err != nil {
    panic(err)
}

defer db.Close()

// авторизация
res, err := db.Query(fmt.Sprintf("SELECT * FROM `users` WHERE `email` = '%s'", email))
result, error := db.Query(fmt.Sprintf("SELECT * FROM `users` WHERE `password` = '%s'", password))
if err != nil || error != nil {
    panic(err)
}

posts = []User{}
for res.Next() {
    var post User
    err = res.Scan(&post.Id, &post.Name, &post.Surname, &post.Email, &post.Number, &post.Password)
    if err != nil {
        panic(err)
    }
    if post.Email != FormEmail {
        fmt.Fprintf(w, "incorrect mail")
    }
}
for result.Next() {
    var post User
    error = res.Scan(&post.Id, &post.Name, &post.Surname, &post.Email, &post.Number, &post.Password)
    if error != nil {
        panic(err)
    }
    if post.Password != FormPassword {
        fmt.Fprintf(w, "incorrect password")
    }
}
http.Redirect(w, r, "/", http.StatusSeeOther)

} html:

    <form action="/log" method="post">
  <input type="email" name="email">
  <input type="password" name="password">
  <button>Login</button>
</form>

思路如下:一个人在表单中输入邮箱地址和密码,你需要查看数据库中的数据。用户是在您从表字段中分配数据的结构之上创建的。 formemail 和 formpassword 是来自表单字段的值,而 email 和 password 最初是字符串类型的空变量。没有显示错误,数据库连接正确。我想我做错了什么,我会感谢每一个提示。如果问题很愚蠢,我很抱歉,我只是在学习

【问题讨论】:

  • emailpassword 永远不会被填充,而是用于从数据库中查询数据。
  • 也使用单个查询并将两个条件都放在那里(邮件和通行证)。这将简化您的代码。那么你只需要检查是否返回结果(有效登录)或不返回(无效)。
  • 请注意,您的密码目前在数据库中是纯文本的。这绝不是一个好主意。它应该使用例如散列bcrypt,并且传输必须加密(https)。

标签: mysql go


【解决方案1】:

我刚刚这样解决了这个问题:

func log(w http.ResponseWriter, r *http.Request) {
    FormEmail := r.FormValue("email")
    FormPassword := r.FormValue("password")
    // connection
    db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:8889)/service")
    if err != nil {
        panic(err)
    }

    defer db.Close()

    // check if there is a user with the specified email + password
    res, err := db.Query(fmt.Sprintf("SELECT * FROM `users` WHERE `email` = '%s' and password = '%s'", FormEmail, FormPassword))
    if err != nil {
        fmt.Printf("%w", err)
        JSONError(500, "DB100 error code example", "DB error", w)
        return
    }
    
    // we check whether there are records in the database.
    if !res.Next() {
        JSONError(413, "trololo", "user not found", w)
        return
    }
    
    http.Redirect(w, r, "/", http.StatusSeeOther)
}


func JSONError(httpcode int, code, msg string, w http.ResponseWriter) {
    type Error struct {
        Code      *string `json:"code,omitempty"`
        Message   *string `json:"message,omitempty"`
    }
    
    w.Header().Set("Content-Type", "application/json; charset=utf-8")
    w.Header().Set("X-Content-Type-Options", "nosniff")
    w.WriteHeader(httpcode)
    json.NewEncoder(w).Encode(
        Error{
            Code:      &code,
            Message:   &msg,
        },
    )
}

请不要重复我的错误

【讨论】:

猜你喜欢
  • 2020-06-19
  • 2012-10-04
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 2012-04-29
  • 2012-03-18
相关资源
最近更新 更多