save username in cookie
This commit is contained in:
parent
539a36dffe
commit
2d98665f92
7 changed files with 149 additions and 95 deletions
|
@ -8,47 +8,39 @@ import (
|
|||
)
|
||||
|
||||
func (wp *Webpage) redirectHome(c *gin.Context) {
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/")
|
||||
}
|
||||
|
||||
func (wp *Webpage) userIsLoggedIn(c *gin.Context) bool {
|
||||
if wp.isLoggedIn {
|
||||
wp.redirectHome(c)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/tasks")
|
||||
}
|
||||
|
||||
func (wp *Webpage) defineRoutes() {
|
||||
|
||||
wp.Router.GET("/", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
||||
"title": "SuperSafe",
|
||||
"loggedIn": wp.isLoggedIn,
|
||||
})
|
||||
wp.Router.GET("/tasks", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "tasks.tmpl", gin.H{"title": "Tasks"})
|
||||
})
|
||||
wp.Router.GET("/login", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "login.tmpl", gin.H{"title": "Login"})
|
||||
})
|
||||
wp.Router.GET("/register", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "register.tmpl", gin.H{"title": "Register"})
|
||||
})
|
||||
|
||||
wp.Router.GET("/login", func(c *gin.Context) {
|
||||
if wp.userIsLoggedIn(c) {
|
||||
wp.Router.POST("/user", func(c *gin.Context) {
|
||||
username, uExisting := c.GetPostForm("username")
|
||||
if uExisting == false || username == "" {
|
||||
c.JSON(400, gin.H{"message": "bad post form"})
|
||||
return
|
||||
}
|
||||
c.HTML(http.StatusOK, "login.tmpl", gin.H{
|
||||
"title": "Login",
|
||||
"loggedIn": wp.isLoggedIn,
|
||||
})
|
||||
success := wp.Database.UserIsLoggedIn(username)
|
||||
c.JSON(200, gin.H{"logged_in": success, "username": username})
|
||||
})
|
||||
|
||||
wp.Router.GET("/logout", func(c *gin.Context) {
|
||||
wp.isLoggedIn = false
|
||||
wp.redirectHome(c)
|
||||
})
|
||||
|
||||
wp.Router.GET("/register", func(c *gin.Context) {
|
||||
wp.userIsLoggedIn(c)
|
||||
c.HTML(http.StatusOK, "register.tmpl", gin.H{
|
||||
"title": "Register",
|
||||
"loggedIn": wp.isLoggedIn,
|
||||
})
|
||||
wp.Router.POST("/logout", func(c *gin.Context) {
|
||||
username, uExisting := c.GetPostForm("username")
|
||||
if uExisting == false || username == "" {
|
||||
c.JSON(400, gin.H{"message": "bad post form"})
|
||||
return
|
||||
}
|
||||
success := wp.Database.LogoutUser(username)
|
||||
c.JSON(200, gin.H{"logged_out": success, "username": username})
|
||||
})
|
||||
|
||||
wp.Router.POST("/login", func(c *gin.Context) {
|
||||
|
@ -58,17 +50,11 @@ func (wp *Webpage) defineRoutes() {
|
|||
c.JSON(400, gin.H{"message": "bad post form"})
|
||||
return
|
||||
}
|
||||
success, err := wp.Database.LoginUser(username, password)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
success := wp.Database.LoginUser(username, password)
|
||||
if success == true {
|
||||
wp.isLoggedIn = true
|
||||
c.JSON(200, gin.H{"message": "login successfull"})
|
||||
c.JSON(200, gin.H{"logged_in": success, "username": username})
|
||||
return
|
||||
}
|
||||
wp.isLoggedIn = false
|
||||
c.JSON(401, gin.H{"message": "user or password not found"})
|
||||
})
|
||||
|
||||
|
|
Reference in a new issue