Circular dependancy issue when working on hardening

This commit is contained in:
2025-03-25 16:43:16 +00:00
parent 1a531af4f8
commit b58a8bdb82
8 changed files with 96 additions and 23 deletions

23
helpers/session.go Normal file
View File

@@ -0,0 +1,23 @@
package helpers
import (
"net/http"
"github.com/gorilla/sessions"
)
var store = sessions.NewCookieStore([]byte("super-secret-key")) // move this here
func init() {
store.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 1,
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteStrictMode,
}
}
func GetSession(w http.ResponseWriter, r *http.Request) (*sessions.Session, error) {
return store.Get(r, "session-name")
}

View File

@@ -2,6 +2,8 @@ package helpers
import (
"html/template"
"net/http"
"synlotto-website/models"
)
func TemplateFuncs() template.FuncMap {
@@ -23,3 +25,24 @@ func TemplateFuncs() template.FuncMap {
},
}
}
func TemplateContext(w http.ResponseWriter, r *http.Request) map[string]interface{} {
session, _ := GetSession(w, r)
var flash string
if f, ok := session.Values["flash"].(string); ok {
flash = f
delete(session.Values, "flash")
session.Save(r, w)
}
var currentUser *models.User
if userId, ok := session.Values["user_id"].(int); ok {
currentUser = models.GetUserByID(userId)
}
return map[string]interface{}{
"Flash": flash,
"User": currentUser,
}
}