package main import ( "context" "net/http" ) type ctxKey string const uidKey ctxKey = "uid" // requireAuth lehnt Anfragen ohne gültige Session ab und legt die uid in den Context. func requireAuth(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { s, ok := getSession(r) if !ok { writeError(w, http.StatusUnauthorized, "Nicht angemeldet") return } ctx := context.WithValue(r.Context(), uidKey, s.UID) next.ServeHTTP(w, r.WithContext(ctx)) }) } func uidFromContext(ctx context.Context) int64 { uid, _ := ctx.Value(uidKey).(int64) return uid }