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.

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大