Implement a stronger, reusable session timeout
This commit is contained in:
@@ -4,11 +4,10 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"synlotto-website/constants"
|
||||
"synlotto-website/helpers"
|
||||
)
|
||||
|
||||
const SessionTimeout = 30 * time.Minute
|
||||
|
||||
func Auth(required bool) func(http.HandlerFunc) http.HandlerFunc {
|
||||
return func(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -23,7 +22,7 @@ func Auth(required bool) func(http.HandlerFunc) http.HandlerFunc {
|
||||
|
||||
if ok {
|
||||
last, hasLast := session.Values["last_activity"].(time.Time)
|
||||
if hasLast && time.Since(last) > SessionTimeout {
|
||||
if hasLast && time.Since(last) > constants.SessionDuration {
|
||||
session.Options.MaxAge = -1
|
||||
session.Save(r, w)
|
||||
|
||||
@@ -43,3 +42,7 @@ func Auth(required bool) func(http.HandlerFunc) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Protected(h http.HandlerFunc) http.HandlerFunc {
|
||||
return Auth(true)(SessionTimeout(h))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user