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
2021const (
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 {
7276func 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