From 36b066c687b051120ac12e653e2408c6f2b462b2 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Sat, 3 Dec 2016 20:30:40 +0100 Subject: [PATCH] Implement logout --- login.go | 4 ++++ main.go | 6 ++++++ templates/login.html | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/login.go b/login.go index 13dc2f2..77217f2 100644 --- a/login.go +++ b/login.go @@ -75,3 +75,7 @@ func loginSuccess(c *gin.Context, username string, name string) { "token": t, }) } + +func clearLogin(c *gin.Context) { + c.SetCookie(authCookie, "", -1, "", "", false, true) +} diff --git a/main.go b/main.go index af6e919..9124d21 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ func main() { router.LoadHTMLGlob("./templates/*") router.Static("/static", "./static") + // Middleware //e.Use(middleware.Logger()) //e.Use(middleware.Recover()) @@ -18,6 +19,7 @@ func main() { router.GET("/login.html", login) a := router.Group("/api/v1") { + a.GET("/logout", logout) a.GET("/login", func(c *gin.Context) { c.Redirect(http.StatusPermanentRedirect, "/login.html") }) @@ -62,6 +64,10 @@ func login(c *gin.Context) { c.HTML(http.StatusOK, "login.html", nil) } +func logout(c *gin.Context) { + clearLogin(c) +} + func loginPost(c *gin.Context) { username, _ := c.GetPostForm("username") password, _ := c.GetPostForm("password") diff --git a/templates/login.html b/templates/login.html index bba40b7..5be9122 100644 --- a/templates/login.html +++ b/templates/login.html @@ -41,7 +41,7 @@ The entered credentials are invalid - +