I am trying to build login pages with Gin-gonic, but I got trouble in redirecting browser.
main.go
router.GET("/login", getLoginPage) router.POST("/login", authentication.Login) router.GET("/dashboard", showMainPage)
If user press the button in html
login.html
<input a href="javascript:void(0);" class="btn btn-primary btn-user btn-block" id="loginButton" value="Login"></input>
Then, Javascript function will be working. The reason why I coded like this is that, I learned that UserID and Password should be hashed before sending to server. (Due to some sort of security problems like tapping, as far as I know)
login.js
document.getElementById("loginButton").addEventListener("click", tryLogin, false); // Get user inputs like ID and PW var request = new XMLHttpRequest(); request.open("POST", "/login"); request.send(formData);
And now, router.POST("/login", authentication.Login)
will work.
auth.go
func Login(c *gin.Context) { id := c.PostForm("id") pw := c.PostForm("pw") // Hash password again, Check Validation and Find user in database // If all inputs are correct, Logged in. c.Header("Content-Type", "text/html") c.HTML(http.StatusOK, "dashboard.html", gin.H{ "title": "title of my page", "username": "I want to send some data like this", "usernickname": "TyeolRik", }) // But the thing is c.HTML not directing as I want. but it returns well checked in Postman. }
But c.HTML doesn't showing rendered HTML screen in browser (Chrome and Edge checked) and also, c.Redirect()
doesn't work neither.
auth.go
func Login(c *gin.Context) { id := c.PostForm("id") pw := c.PostForm("pw") // Hash password again, Check Validation and Find user in database c.Redirect(http.StatusMovedPermanently, "/dashboard") }
How can I redirect HTML easily? I temporarily used window.location.href = '/dashboard';
in Javascript. But It cannot be rendered like using gin.H{something}
Is there any tips for me?