在登录之前,请检查登录值。

huangapple go评论127阅读模式
英文:

Go checking login values before login

问题

我有一个登录表单,当我点击发送时,页面会重定向到/create。在那里,我会在一个GO函数中检查我的登录值是否正确,以便给予下一页的访问权限,但是我的函数有问题。

loginCheck:

  1. func loginCheck(w http.ResponseWriter, r* http.Request){
  2. r.ParseForm()
  3. //调用DB函数
  4. db:= SetupDB()
  5. name, password := r.FormValue("user"), r.FormValue("password")
  6. hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  7. PanicIf(err)
  8. rows, err := db.Query("SELECT name, password from users where name = $1 and password = $2" ,name, hashedPassword)
  9. PanicIf(err)
  10. defer rows.Close()
  11. for rows.Next() {
  12. err := rows.Scan(&name, &hashedPassword)
  13. PanicIf(err)
  14. fmt.Println(name, hashedPassword)
  15. }
  16. db.Close()
  17. }

我试图在rows.Next()中打印值,以查看是否从数据库中读取了值,但是它是空的。

英文:

I got a login form, when I click on send, I redirect the page to a /create. There I check in a GO function if the values of my login are the correct to give access to the next page, but I have a problem with my function.

loginCheck:

  1. func loginCheck(w http.ResponseWriter, r* http.Request){
  2. r.ParseForm()
  3. //Call the DB function
  4. db:= SetupDB()
  5. name, password := r.FormValue("user"), r.FormValue("password")
  6. hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  7. PanicIf(err)
  8. rows, err := db.Query("SELECT name, password from users where name = $1 and password = $2" ,name, hashedPassword)
  9. PanicIf(err)
  10. defer rows.Close()
  11. for rows.Next() {
  12. err := rows.Scan(&name, &hashedPassword)
  13. PanicIf(err)
  14. fmt.Println(name, hashedPassword)
  15. }
  16. db.Close()
  17. }

Im trying to print the values inside the rows.Next() to see if read the values from the database but it's empty.

答案1

得分: 5

为了补充seong评论,你可以考虑参考goserver中的这个例子

  1. func CheckPassword(username, password string) bool {
  2. if len(username) == 0 || len(password) == 0 {
  3. return false
  4. }
  5. var hashPw []byte
  6. err := DB.QueryRow("SELECT password FROM users WHERE username=?", username).Scan(&hashPw)
  7. if err != nil {
  8. log.Println("CheckPassword", err.Error())
  9. return false
  10. }
  11. if len(hashPw) == 0 {
  12. return false
  13. }
  14. err = bcrypt.CompareHashAndPassword(hashPw, []byte(password))
  15. if err == nil {
  16. return true
  17. }
  18. return false
  19. }

查询只使用了username

英文:

To add to seong's comment, you can consider this example in goserver:

  1. func CheckPassword(username, password string) bool {
  2. if len(username) == 0 || len(password) == 0 {
  3. return false
  4. }
  5. var hashPw []byte
  6. err := DB.QueryRow("SELECT password FROM users WHERE username=?", username).Scan(&hashPw)
  7. if err != nil {
  8. log.Println("CheckPassword", err.Error())
  9. return false
  10. }
  11. if len(hashPw) == 0 {
  12. return false
  13. }
  14. err = bcrypt.CompareHashAndPassword(hashPw, []byte(password))
  15. if err == nil {
  16. return true
  17. }
  18. return false
  19. }

The query is done using only username.

huangapple
  • 本文由 发表于 2014年7月15日 19:22:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/24756912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定