Skip to content

Commit f797e28

Browse files
committed
fix: Admin users could not access orders
1 parent 5a86820 commit f797e28

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

internal/interfaces/api/handler/order_handler.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/zenfulcode/commercify/internal/domain/entity"
1111
"github.com/zenfulcode/commercify/internal/dto"
1212
"github.com/zenfulcode/commercify/internal/infrastructure/logger"
13+
"github.com/zenfulcode/commercify/internal/interfaces/api/middleware"
1314
)
1415

1516
// OrderHandler handles order-related HTTP requests
@@ -29,7 +30,10 @@ func NewOrderHandler(orderUseCase *usecase.OrderUseCase, logger logger.Logger) *
2930
// GetOrder handles getting an order by ID
3031
func (h *OrderHandler) GetOrder(w http.ResponseWriter, r *http.Request) {
3132
// Get user ID from context
32-
userID, ok := r.Context().Value("user_id").(uint)
33+
userID, ok := r.Context().Value(middleware.UserIDKey).(uint)
34+
35+
h.logger.Debug("User ID from context: %d", userID)
36+
3337
if !ok {
3438
h.logger.Error("Unauthorized access attempt")
3539
http.Error(w, "Unauthorized", http.StatusUnauthorized)
@@ -58,8 +62,8 @@ func (h *OrderHandler) GetOrder(w http.ResponseWriter, r *http.Request) {
5862

5963
// Check if the user is authorized to view this order
6064
if order.UserID != userID {
61-
role, ok := r.Context().Value("role").(string)
62-
if !ok || role != "admin" {
65+
role, ok := r.Context().Value(middleware.RoleKey).(string)
66+
if !ok || role != string(entity.RoleAdmin) {
6367
h.logger.Error("Unauthorized access to order %d by user %d", order.ID, userID)
6468
response := dto.ErrorResponse("You are not authorized to view this order")
6569
w.Header().Set("Content-Type", "application/json")

internal/interfaces/api/middleware/auth_middleware.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"strings"
77

8+
"github.com/zenfulcode/commercify/internal/domain/entity"
89
"github.com/zenfulcode/commercify/internal/infrastructure/auth"
910
"github.com/zenfulcode/commercify/internal/infrastructure/logger"
1011
)
@@ -20,7 +21,7 @@ type contextKey string
2021
const (
2122
UserIDKey contextKey = "user_id"
2223
emailKey contextKey = "email"
23-
roleKey contextKey = "role"
24+
RoleKey contextKey = "role"
2425
)
2526

2627
// NewAuthMiddleware creates a new AuthMiddleware
@@ -61,7 +62,10 @@ func (m *AuthMiddleware) Authenticate(next http.Handler) http.Handler {
6162
// Add user info to request context
6263
ctx := context.WithValue(r.Context(), UserIDKey, claims.UserID)
6364
ctx = context.WithValue(ctx, emailKey, claims.Email)
64-
ctx = context.WithValue(ctx, roleKey, claims.Role)
65+
ctx = context.WithValue(ctx, RoleKey, claims.Role)
66+
67+
// print user ID and role for debugging
68+
m.logger.Debug("Authenticated user ID: %d, Role: %s", claims.UserID, claims.Role)
6569

6670
// Call the next handler with the updated context
6771
next.ServeHTTP(w, r.WithContext(ctx))
@@ -72,8 +76,8 @@ func (m *AuthMiddleware) Authenticate(next http.Handler) http.Handler {
7276
func AdminOnly(next http.Handler) http.Handler {
7377
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7478
// Get role from context
75-
role, ok := r.Context().Value(roleKey).(string)
76-
if !ok || role != "admin" {
79+
role, ok := r.Context().Value(RoleKey).(string)
80+
if !ok || role != string(entity.RoleAdmin) {
7781
http.Error(w, "Admin access required", http.StatusForbidden)
7882
return
7983
}

0 commit comments

Comments
 (0)