fix: unify tenant context keys and load tenant_id from JWT
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"aggios-app/backend/internal/api/middleware"
|
||||||
"aggios-app/backend/internal/repository"
|
"aggios-app/backend/internal/repository"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -58,14 +59,12 @@ func (h *AgencyHandler) GetProfile(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get tenant from context (set by middleware)
|
// Get tenant from context (set by auth middleware)
|
||||||
tenantID := r.Context().Value("tenantID")
|
tenantID := r.Context().Value(middleware.TenantIDKey)
|
||||||
log.Printf("DEBUG GetProfile: tenantID from context = %v (type: %T)", tenantID, tenantID)
|
log.Printf("DEBUG GetProfile: tenantID from context = %v (type: %T)", tenantID, tenantID)
|
||||||
|
|
||||||
if tenantID == nil {
|
if tenantID == nil {
|
||||||
log.Printf("DEBUG GetProfile: tenantID is nil, checking subdomain from context")
|
log.Printf("DEBUG GetProfile: tenantID is nil from auth middleware")
|
||||||
subdomain := r.Context().Value("subdomain")
|
|
||||||
log.Printf("DEBUG GetProfile: subdomain = %v", subdomain)
|
|
||||||
http.Error(w, "Tenant not found in context", http.StatusUnauthorized)
|
http.Error(w, "Tenant not found in context", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -115,8 +114,8 @@ func (h *AgencyHandler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get tenant from context
|
// Get tenant from context (set by auth middleware)
|
||||||
tenantID := r.Context().Value("tenantID")
|
tenantID := r.Context().Value(middleware.TenantIDKey)
|
||||||
if tenantID == nil {
|
if tenantID == nil {
|
||||||
http.Error(w, "Tenant not found", http.StatusUnauthorized)
|
http.Error(w, "Tenant not found", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
type contextKey string
|
type contextKey string
|
||||||
|
|
||||||
const UserIDKey contextKey = "userID"
|
const UserIDKey contextKey = "userID"
|
||||||
|
const TenantIDKey contextKey = "tenantID"
|
||||||
|
|
||||||
// Auth validates JWT tokens
|
// Auth validates JWT tokens
|
||||||
func Auth(cfg *config.Config) func(http.Handler) http.Handler {
|
func Auth(cfg *config.Config) func(http.Handler) http.Handler {
|
||||||
@@ -46,7 +47,9 @@ func Auth(cfg *config.Config) func(http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
userID := claims["user_id"].(string)
|
userID := claims["user_id"].(string)
|
||||||
|
tenantID := claims["tenant_id"].(string)
|
||||||
ctx := context.WithValue(r.Context(), UserIDKey, userID)
|
ctx := context.WithValue(r.Context(), UserIDKey, userID)
|
||||||
|
ctx = context.WithValue(ctx, TenantIDKey, tenantID)
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,7 @@ import (
|
|||||||
"aggios-app/backend/internal/repository"
|
"aggios-app/backend/internal/repository"
|
||||||
)
|
)
|
||||||
|
|
||||||
type tenantContextKey string
|
const SubdomainKey contextKey = "subdomain"
|
||||||
|
|
||||||
const TenantIDKey tenantContextKey = "tenantID"
|
|
||||||
const SubdomainKey tenantContextKey = "subdomain"
|
|
||||||
|
|
||||||
// TenantDetector detects tenant from subdomain
|
// TenantDetector detects tenant from subdomain
|
||||||
func TenantDetector(tenantRepo *repository.TenantRepository) func(http.Handler) http.Handler {
|
func TenantDetector(tenantRepo *repository.TenantRepository) func(http.Handler) http.Handler {
|
||||||
|
|||||||
Reference in New Issue
Block a user