I'm having a bit of trouble in logging in using gin-gonic in making a web application. I'm currently using an API for it. where I can make requests for entry. This is my code snippets:
main
// LOGIN
router.GET("/login", loginUser)
router.POST("/login", login)
snippet for login function
// 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()
}
As you can see im trying to post request my credentials in order to login.
When I click submit on my form box in function loginUser- login recieves the post request and then redirects itself to the html when success and the 404error html if wrong.
I dont know if my understanding is right, it's my second day learning this to make web apps. Help pls