From 3d3fb3c826e783b1c30fdbbeb623211c0b69ca5f Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Tue, 12 Mar 2024 20:27:39 +0100 Subject: [PATCH] Added logout --- cmd/main.go | 1 + cmd/view/sessions.go | 22 ++++++++++++++++++++++ web/templates/edit-user.html | 2 +- web/templates/index.html | 1 + 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/cmd/main.go b/cmd/main.go index 97a0d4a..16ecaeb 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -49,6 +49,7 @@ func main() { mux.HandleFunc("GET /create-user/", view.CreateUser) mux.HandleFunc("GET /edit-user/", view.EditUser(db, store)) mux.HandleFunc("GET /hub/", view.ShowHub(db, store)) + mux.HandleFunc("GET /logout/", view.Logout(store)) mux.HandleFunc("GET /rejected-articles/", view.ShowRejectedArticles(db, store)) mux.HandleFunc("GET /rss/", view.ShowRSS( db, diff --git a/cmd/view/sessions.go b/cmd/view/sessions.go index 0098147..2a34130 100644 --- a/cmd/view/sessions.go +++ b/cmd/view/sessions.go @@ -88,3 +88,25 @@ func Login(db *model.DB, s *control.CookieStore) http.HandlerFunc { template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user.Role) } } + +func Logout(s *control.CookieStore) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + session, err := s.Get(r, "cookie") + if err != nil { + tmpl, err := template.ParseFiles("web/templates/login.html") + msg := "Session nicht mehr gültig. Bitte erneut anmelden." + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) + } + + session.Options.MaxAge = -1 + + if err = session.Save(r, w); err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + tmpl, err := template.ParseFiles("web/templates/login.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) + } +} diff --git a/web/templates/edit-user.html b/web/templates/edit-user.html index 71a6bec..3bce732 100644 --- a/web/templates/edit-user.html +++ b/web/templates/edit-user.html @@ -8,6 +8,6 @@ - + {{end}} diff --git a/web/templates/index.html b/web/templates/index.html index 94c2053..dcc6329 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -10,6 +10,7 @@

Orient Editor

+
{{template "page-content" .}}