diff --git a/serve/login.go b/serve/login.go index a876509..410ac83 100644 --- a/serve/login.go +++ b/serve/login.go @@ -14,7 +14,7 @@ import ( func handleLoginPage(w http.ResponseWriter, r *http.Request) { // Если пользователь уже авторизован, перенаправляем на главную if isAuthenticated(r) { - http.Redirect(w, r, "/", http.StatusSeeOther) + http.Redirect(w, r, makePath("/"), http.StatusSeeOther) return } @@ -41,6 +41,7 @@ func handleLoginPage(w http.ResponseWriter, r *http.Request) { Message: "", Error: "", CSRFToken: csrfToken, + BasePath: getBasePath(), } if err := RenderLoginPage(w, data); err != nil { @@ -73,6 +74,7 @@ type LoginPageData struct { Message string Error string CSRFToken string + BasePath string } // RenderLoginPage рендерит страницу входа diff --git a/serve/middleware.go b/serve/middleware.go index 06dda10..65af941 100644 --- a/serve/middleware.go +++ b/serve/middleware.go @@ -2,6 +2,7 @@ package serve import ( "net/http" + "strings" "github.com/direct-dev-ru/linux-command-gpt/config" ) @@ -15,8 +16,8 @@ func AuthMiddleware(next http.HandlerFunc) http.HandlerFunc { return } - // Исключаем страницу входа и API логина из проверки - if r.URL.Path == "/login" || r.URL.Path == "/api/login" || r.URL.Path == "/api/validate-token" { + // Исключаем страницу входа и API логина из проверки (с учетом BasePath) + if r.URL.Path == makePath("/login") || r.URL.Path == makePath("/api/login") || r.URL.Path == makePath("/api/validate-token") { next(w, r) return } @@ -31,8 +32,8 @@ func AuthMiddleware(next http.HandlerFunc) http.HandlerFunc { return } - // Для веб-запросов перенаправляем на страницу входа - http.Redirect(w, r, "/login", http.StatusSeeOther) + // Для веб-запросов перенаправляем на страницу входа (с учетом BasePath) + http.Redirect(w, r, makePath("/login"), http.StatusSeeOther) return } @@ -50,8 +51,8 @@ func CSRFMiddleware(next http.HandlerFunc) http.HandlerFunc { return } - // Исключаем некоторые API endpoints - if r.URL.Path == "/api/login" || r.URL.Path == "/api/logout" { + // Исключаем некоторые API endpoints (с учетом BasePath) + if r.URL.Path == makePath("/api/login") || r.URL.Path == makePath("/api/logout") { next(w, r) return } @@ -103,7 +104,8 @@ func CSRFMiddleware(next http.HandlerFunc) http.HandlerFunc { // isAPIRequest проверяет, является ли запрос API запросом func isAPIRequest(r *http.Request) bool { path := r.URL.Path - return len(path) >= 4 && path[:4] == "/api" + apiPrefix := makePath("/api") + return strings.HasPrefix(path, apiPrefix) } // RequireAuth обертка для requireAuth из auth.go diff --git a/serve/templates/login.go b/serve/templates/login.go index 7584669..8435a1f 100644 --- a/serve/templates/login.go +++ b/serve/templates/login.go @@ -285,7 +285,7 @@ const LoginPageTemplate = ` try { const csrfToken = document.getElementById('csrf_token').value; - const response = await fetch('/api/login', { + const response = await fetch('{{.BasePath}}/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -302,7 +302,7 @@ const LoginPageTemplate = ` if (data.success) { // Успешная авторизация, перенаправляем на главную страницу - window.location.href = '/'; + window.location.href = '{{.BasePath}}/'; } else { // Ошибка авторизации showMessage(data.error || 'Ошибка авторизации', 'error');