From 96f81418ecfd7c34b6902c928e6f8ea040795ed8 Mon Sep 17 00:00:00 2001 From: vinchent Date: Mon, 22 Jul 2024 10:00:17 +0200 Subject: [PATCH] Checking to see if a user is logged in, and logging a user our --- cmd/web/routes.go | 1 + internal/handlers/handlers.go | 8 ++++++++ internal/models/templatedata.go | 19 ++++++++++--------- internal/render/render.go | 3 +++ templates/base.layout.tmpl | 4 ++++ 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/cmd/web/routes.go b/cmd/web/routes.go index f8eaaa4..e1deeb2 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -32,6 +32,7 @@ func routes(app *config.AppConfig) http.Handler { mux.Get("/book-room", handlers.Repo.BookRoom) mux.Get("/user/login", handlers.Repo.ShowLogin) mux.Post("/user/login", handlers.Repo.PostShowLogin) + mux.Get("/user/logout", handlers.Repo.Logout) fileServer := http.FileServer(http.Dir("./static/")) mux.Handle("/static/*", http.StripPrefix("/static", fileServer)) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 3bbb458..59e0d5b 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -494,3 +494,11 @@ func (m *Repository) PostShowLogin(w http.ResponseWriter, r *http.Request) { m.App.Session.Put(r.Context(), "flash", "Logged in successfully") http.Redirect(w, r, "/", http.StatusSeeOther) } + +// Logout logs a user out +func (m *Repository) Logout(w http.ResponseWriter, r *http.Request) { + // TODO Use Redis to store the session. Check the documentation of scs package + m.App.Session.Destroy(r.Context()) + m.App.Session.RenewToken(r.Context()) + http.Redirect(w, r, "/user/login", http.StatusSeeOther) +} diff --git a/internal/models/templatedata.go b/internal/models/templatedata.go index a852bf9..7f68f9b 100644 --- a/internal/models/templatedata.go +++ b/internal/models/templatedata.go @@ -4,13 +4,14 @@ import "go-udemy-web-1/internal/forms" // TemplateData holds data sent from handlers to templates type TemplateData struct { - StringMap map[string]string - IntMap map[string]int - FloatMap map[string]float32 - Data map[string]interface{} - CSRFToken string - Flash string - Warning string - Error string - Form *forms.Form + StringMap map[string]string + IntMap map[string]int + FloatMap map[string]float32 + Data map[string]interface{} + Form *forms.Form + CSRFToken string + Flash string + Warning string + Error string + IsAuthenticated int } diff --git a/internal/render/render.go b/internal/render/render.go index e6944d2..f569d8c 100644 --- a/internal/render/render.go +++ b/internal/render/render.go @@ -32,6 +32,9 @@ func AddDefaultData(td *models.TemplateData, r *http.Request) *models.TemplateDa td.Warning = app.Session.PopString(r.Context(), "warning") td.Error = app.Session.PopString(r.Context(), "error") td.CSRFToken = nosurf.Token(r) + if app.Session.Exists(r.Context(), "user_id") { + td.IsAuthenticated = 1 + } return td } diff --git a/templates/base.layout.tmpl b/templates/base.layout.tmpl index 7add260..24da16e 100644 --- a/templates/base.layout.tmpl +++ b/templates/base.layout.tmpl @@ -51,7 +51,11 @@ Contact