From 47fba49d1835442f096739b92a3dd3ed9476470b Mon Sep 17 00:00:00 2001
From: Roman Sergienko <romasergienko@outlook.com>
Date: Fri, 04 Dec 2026 11:14:16 +0000
Subject: [PATCH] Fix API error handling: return JSON on auth failure for API routes

---
 web/auth.py |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/web/auth.py b/web/auth.py
index bf80c2d..b6dae94 100644
--- a/web/auth.py
+++ b/web/auth.py
@@ -101,7 +101,13 @@
         "/miniapp",
         "/miniapp/login",
     ) or request.path.startswith("/static")
+    
+    # Для API запросов возвращаем JSON вместо редиректа
+    is_api = request.path.startswith("/api/")
+    
     if request["user"] is None and not public:
+        if is_api:
+            return web.json_response({"ok": False, "msg": "Не авторизован."}, status=401)
         raise web.HTTPFound("/login")
     try:
         response = await handler(request)

--
Gitblit v1.9.3