Circular dependancy issue when working on hardening
This commit is contained in:
23
helpers/session.go
Normal file
23
helpers/session.go
Normal 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")
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user