package middleware import ( "context" "net/http" ) type contextKey string const ( UserIDKey contextKey = "userID" IsAuthKey contextKey = "isAuthenticated" ) // AuthMiddlewareWithContext добавляет информацию об авторизации в контекст func AuthMiddlewareWithContext(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Создаем контекст с тестовыми данными ctx := r.Context() ctx = context.WithValue(ctx, UserIDKey, 0) ctx = context.WithValue(ctx, IsAuthKey, false) // В реальном проекте здесь будет: // token := r.Header.Get("Authorization") // if token != "" { // userID, err := validateToken(token) // if err == nil { // ctx = context.WithValue(ctx, UserIDKey, userID) // ctx = context.WithValue(ctx, IsAuthKey, true) // } // } next.ServeHTTP(w, r.WithContext(ctx)) }) }