From e4bef7006c45d9a281f1d61add434049a9836c5c Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sat, 28 Sep 2024 13:44:25 +0200 Subject: [PATCH] Abstract sessions.Session with b.session --- cmd/backend/sessions.go | 5 ++++- cmd/frontend/sessions.go | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/cmd/backend/sessions.go b/cmd/backend/sessions.go index eb55dfc..9fbf3f9 100644 --- a/cmd/backend/sessions.go +++ b/cmd/backend/sessions.go @@ -10,7 +10,10 @@ import ( "github.com/gorilla/sessions" ) -type CookieStore struct{ sessions.CookieStore } +type ( + CookieStore struct{ sessions.CookieStore } + Session struct{ sessions.Session } +) func NewKey() ([]byte, error) { key := make([]byte, 32) diff --git a/cmd/frontend/sessions.go b/cmd/frontend/sessions.go index d88e595..b2f94de 100644 --- a/cmd/frontend/sessions.go +++ b/cmd/frontend/sessions.go @@ -7,7 +7,6 @@ import ( "log" "net/http" - "github.com/gorilla/sessions" b "streifling.com/jason/cpolis/cmd/backend" ) @@ -29,16 +28,17 @@ func saveSession(w http.ResponseWriter, r *http.Request, s *b.CookieStore, u *b. } // getSession is used for verifying that the user is logged in and returns their session and an error. -func getSession(w http.ResponseWriter, r *http.Request, c *b.Config, s *b.CookieStore) (*sessions.Session, error) { +func getSession(w http.ResponseWriter, r *http.Request, c *b.Config, s *b.CookieStore) (*b.Session, error) { msg := "Keine gültige Session. Bitte erneut anmelden." tmpl, tmplErr := template.ParseFiles(c.WebDir+"/templates/index.html", c.WebDir+"/templates/login.html") - session, err := s.Get(r, "cookie") + tmpSession, err := s.Get(r, "cookie") if err != nil { template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg) return nil, err } + session := &b.Session{Session: *tmpSession} if session.IsNew { template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg) return session, errors.New("error: no existing session")