package sessions import ( "context" "net/http" ) func SetSession(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { scookie, err := r.Cookie("session_token") if err != nil || scookie.Value == "" { // no session value or cookie next.ServeHTTP(w, r) } cvalue := scookie.Value vsession, ok := Sessions[cvalue] if !ok { // no session next.ServeHTTP(w, r) } // set session ctx := context.WithValue(r.Context(), SessionCtxKey("session"), vsession) next.ServeHTTP(w, r.WithContext(ctx)) }) } func GuestSession(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // if SessionKey does not exist then this is a valid guest request if _, ok := r.Context().Value(SessionCtxKey("session")).(session); !ok { next.ServeHTTP(w, r) } // else redirect to `/` as this is an auth session http.Redirect(w, r, "/", http.StatusSeeOther) }) }