Abstract sessions.Session with b.session

This commit is contained in:
Jason Streifling 2024-09-28 13:44:25 +02:00
parent afa1b65563
commit e4bef7006c
2 changed files with 7 additions and 4 deletions

View File

@ -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)

View File

@ -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")