duanpa1980 2019-02-07 15:32
浏览 84
已采纳

无法访问Golang中的登录页面

I am new to Golang. I have made a demo app for practice in which i have login register and homepage. When i go to login page it redirects to home page . I am not understanding what is happening.

This is my go code

package main

import (
    "database/sql"
    "fmt"
    "net/http"

    _ "github.com/go-sql-driver/mysql"
    "golang.org/x/crypto/bcrypt"
)

var db *sql.DB
var err error

func signupPage(res http.ResponseWriter, req *http.Request) {
    fmt.Println("entered Signup")
    if req.Method != "POST" {
        http.ServeFile(res, req, "template/signup.html")
        return
    }

    email := req.FormValue("email")
    password := req.FormValue("password")

    var user string

    err := db.QueryRow("SELECT email FROM users WHERE email=?", email).Scan(&user)

    switch {
    case err == sql.ErrNoRows:
        hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
        if err != nil {
            http.Error(res, "Server error, unable to create your account.", 500)
            return
        }

        _, err = db.Exec("INSERT INTO users(email, password) VALUES(?, ?)", email, hashedPassword)
        if err != nil {
            http.Error(res, "Server error, unable to create your account.", 500)
        }
        res.Write([]byte("User Registered Successfully"))
        return

    case err != nil:
        http.Error(res, "Server error, unable to create your account.", 500)
        return

    default:
        http.Redirect(res, req, "/", 301)
    }

}

func loginPage(res http.ResponseWriter, req *http.Request) {
    fmt.Println("Entered login")
    if req.Method != "POST" {
        http.ServeFile(res, req, "template/login.html")
        return
    }

    email := req.FormValue("email")
    password := req.FormValue("password")

    var dbemail string
    var dbpassword string

    err := db.QueryRow("SELECT email, password FORM users WHERE email=?", email).Scan(&dbemail, &dbpassword)
    if err != nil {
        http.Redirect(res, req, "/login", 301)
        return
    }

    err = bcrypt.CompareHashAndPassword([]byte(dbpassword), []byte(password))
    if err != nil {
        http.Redirect(res, req, "/login", 301)
        return
    }

    res.Write([]byte("Hello" + dbemail))

}

func homePage(res http.ResponseWriter, req *http.Request) {
    http.ServeFile(res, req, "template/landing.html")
}

func main() {
    db, err = sql.Open("mysql", "root:password@/golang_demo")
    if err != nil {
        panic(err.Error())
    } else {
        fmt.Println("Database connected successfully")
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
        panic(err.Error())
    }
    http.HandleFunc("/login", loginPage)
    http.HandleFunc("/", homePage)
    http.HandleFunc("/signup", signupPage)

    http.ListenAndServe(":9090", nil)
}

When i go to signup page it goes successfully. But when i go to login page it is redirecting me to home page. Please help!

  • 写回答

1条回答 默认 最新

  • doumu8217 2019-02-07 18:34
    关注

    This is just an educated guess, since from the code I am seeing it doesn't look like that should happen. Since you are using 301 redirects (Moved Permanently), I would guess that at some point in writing your function and testing it, you did a redirect to the home page. Since it is "moved permanently", the browser now doesn't even ask the server if it should redirect when it sees that URL, it just does it.

    If you're in chrome, open the dev tools, and disabling the cache should resolve it. Or even better, try it in a different browser that you haven't used on this site yet, and see if it works there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮