diff --git a/pkg/user/api/auth_handler.go b/pkg/user/api/auth_handler.go index 86691c7..92bdc0a 100644 --- a/pkg/user/api/auth_handler.go +++ b/pkg/user/api/auth_handler.go @@ -74,7 +74,7 @@ type LoginResponse struct { // handleLogin godoc // // @Summary User login -// @Description Authenticate user and return JWT token +// @Description Authenticate user or admin and return JWT token. Supports both regular users and admin authentication. // @Tags API/v1/User // @Accept json // @Produce json @@ -101,16 +101,27 @@ func (h *AuthHandler) handleLogin(w http.ResponseWriter, r *http.Request) { } } - // Authenticate user - user, err := h.authService.Authenticate(ctx, req.Username, req.Password) - if err != nil { - log.Trace().Ctx(ctx).Err(err).Str("username", req.Username).Msg("Authentication failed") + // Try unified authentication (regular user first, then admin fallback) + var authenticatedUser *user.User + var authError error + + // Try regular user authentication first + authenticatedUser, authError = h.authService.Authenticate(ctx, req.Username, req.Password) + + // If regular auth fails, try admin authentication + if authError != nil { + authenticatedUser, authError = h.authService.AdminAuthenticate(ctx, req.Password) + } + + // If both authentication methods failed + if authError != nil { + log.Trace().Ctx(ctx).Err(authError).Str("username", req.Username).Msg("Authentication failed") http.Error(w, `{"error":"invalid_credentials","message":"Invalid username or password"}`, http.StatusUnauthorized) return } - // Generate JWT token - token, err := h.authService.GenerateJWT(ctx, user) + // Generate JWT token using the authenticated user (regular or admin) + token, err := h.authService.GenerateJWT(ctx, authenticatedUser) if err != nil { log.Error().Ctx(ctx).Err(err).Msg("Failed to generate JWT token") http.Error(w, `{"error":"server_error","message":"Failed to generate authentication token"}`, http.StatusInternalServerError) diff --git a/pkg/user/sqlite_repository.go b/pkg/user/sqlite_repository.go index 7556fd8..4e959d6 100644 --- a/pkg/user/sqlite_repository.go +++ b/pkg/user/sqlite_repository.go @@ -10,6 +10,7 @@ import ( "time" "dance-lessons-coach/pkg/config" + "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace"