diff --git a/internal/helpers/http/session.go b/internal/helpers/http/session.go deleted file mode 100644 index 8faa9db..0000000 --- a/internal/helpers/http/session.go +++ /dev/null @@ -1,51 +0,0 @@ -package helpers - -import ( - "net/http" - "time" - - session "synlotto-website/internal/handlers/session" - - "synlotto-website/internal/platform/constants" - - "github.com/gorilla/sessions" -) - -func GetSession(w http.ResponseWriter, r *http.Request) (*sessions.Session, error) { - return session.GetSession(w, r) -} - -func IsSessionExpired(session *sessions.Session) bool { - last, ok := session.Values["last_activity"].(time.Time) - if !ok { - return false - } - return time.Since(last) > constants.SessionDuration -} - -func UpdateSessionActivity(session *sessions.Session, r *http.Request, w http.ResponseWriter) { - session.Values["last_activity"] = time.Now().UTC() - session.Save(r, w) -} - -func AuthMiddleware(next http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - session, _ := GetSession(w, r) - - if IsSessionExpired(session) { - session.Options.MaxAge = -1 - session.Save(r, w) - - newSession, _ := GetSession(w, r) - newSession.Values["flash"] = "Your session has timed out." - newSession.Save(r, w) - - http.Redirect(w, r, "/account/login", http.StatusSeeOther) - return - } - - UpdateSessionActivity(session, r, w) - - next(w, r) - } -} diff --git a/internal/platform/constants/session.go b/internal/platform/constants/session.go deleted file mode 100644 index ef37552..0000000 --- a/internal/platform/constants/session.go +++ /dev/null @@ -1,5 +0,0 @@ -package constants - -import "time" - -const SessionDuration = 30 * time.Minute