From 04283d5917d16ec64ee903fde0afb1c1a5fa8359 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sun, 19 Jan 2025 20:29:10 +0100 Subject: [PATCH] Restore old way of managing session while keeping slimmed down version --- cmd/frontend/sessions.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cmd/frontend/sessions.go b/cmd/frontend/sessions.go index 71c7d94..153a439 100644 --- a/cmd/frontend/sessions.go +++ b/cmd/frontend/sessions.go @@ -84,19 +84,27 @@ func ValidateSession(w http.ResponseWriter, r *http.Request, c *b.Config, s map[ // their session and an error. It also handles cases where the user is not // logged in. func ManageSession(w http.ResponseWriter, r *http.Request, c *b.Config, s map[string]*Session) (*Session, error) { - session, err := ValidateSession(w, r, c, s) - if err != nil { - if session.cookie != nil { - session.cookie.Expires = time.Now() - http.SetCookie(w, session.cookie) - } + tmpl, tmplErr := template.ParseFiles(filepath.Join(c.WebDir, "templates", "index.html"), filepath.Join(c.WebDir, "templates", "login.html")) - tmpl, tmplErr := template.ParseFiles(filepath.Join(c.WebDir, "templates", "index.html"), filepath.Join(c.WebDir, "templates", "login.html")) + cookie, err := r.Cookie("cpolis_session") + if err != nil { if err = template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", nil); err != nil { return nil, fmt.Errorf("error executing template: %v", err) } - return nil, err + return nil, errors.New("no cookie set") + } + + session, ok := s[cookie.Value] + if !ok { + cookie.Expires = time.Now() + http.SetCookie(w, cookie) + + if err = template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", nil); err != nil { + return nil, fmt.Errorf("error executing template: %v", err) + } + + return nil, errors.New("session does not exist") } session.cookie.Expires = time.Now().Add(time.Hour * time.Duration(c.CookieExpiryHours))