gpt4 book ai didi

使用 GIn-Gonic 进行用户登录

转载 作者:数据小太阳 更新时间:2023-10-29 03:22:33 26 4
gpt4 key购买 nike

我在制作 Web 应用程序时使用 gin-gonic 登录时遇到了一些麻烦。我目前正在为此使用 API。我可以在那里提出进入请求。这是我的代码片段:

主要

// LOGIN
router.GET("/login", loginUser)
router.POST("/login", login)

登录功能的代码段

// LOGIN FUNCTIONS

func loginUser(c *gin.Context) {
c.HTML(200, "login.html", gin.H{})

}

func login(c *gin.Context) {


email := c.PostForm("email")
password := c.PostForm("password")



r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.POST("/api/user/login", func(c *gin.Context) {
var json common.Login
if err := common.Bind(c, &json); err == nil {

if json.Email == email && json.Password == password {
c.JSON(200, gin.H{"email":email, "password": password,})
fmt.Println(email)
fmt.Println(password)

} else {
c.JSON(401, common.NewError("user", errors.New("wrong username or password")))
c.HTML(404, "404notification.html", gin.H{})

}
c.HTML(200, "notification.html", gin.H{"email": email})

} else {
c.JSON(422, common.NewValidatorError(err))
}
})
r.Run()




}

如您所见,我正在尝试发布请求我的凭据以便登录。

当我在函数 loginUser 中单击我的表单框上的提交时,login 收到发布请求,然后在成功时将自身重定向到 html,如果错误则重定向到 404error html。

我不知道我的理解是否正确,这是我第二天学习这个来制作网络应用程序。请帮忙

最佳答案

您使用文件扩展名 (.html)。是否要使用母版页?

如果你想使用母版页,那么你不需要写任何扩展名,例如“home/index”。

如果你不想使用母版页那么你写完整路径“home/index.html”

关于使用 GIn-Gonic 进行用户登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51119676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com