Files
website/handlers/session.go

29 lines
510 B
Go

package handlers
import (
"net/http"
"synlotto-website/helpers"
)
func GetCurrentUserID(r *http.Request) (int, bool) {
session, err := helpers.GetSession(nil, r)
if err != nil {
return 0, false
}
id, ok := session.Values["user_id"].(int)
return id, ok
}
func RequireAuth(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, ok := GetCurrentUserID(r)
if !ok {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
next(w, r)
}
}