Merged main into release while building v2.0.4

This commit is contained in:
3 changed files with 14 additions and 10 deletions

View File

@@ -14,7 +14,7 @@ import (
func handleLoginPage(w http.ResponseWriter, r *http.Request) { func handleLoginPage(w http.ResponseWriter, r *http.Request) {
// Если пользователь уже авторизован, перенаправляем на главную // Если пользователь уже авторизован, перенаправляем на главную
if isAuthenticated(r) { if isAuthenticated(r) {
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, makePath("/"), http.StatusSeeOther)
return return
} }
@@ -41,6 +41,7 @@ func handleLoginPage(w http.ResponseWriter, r *http.Request) {
Message: "", Message: "",
Error: "", Error: "",
CSRFToken: csrfToken, CSRFToken: csrfToken,
BasePath: getBasePath(),
} }
if err := RenderLoginPage(w, data); err != nil { if err := RenderLoginPage(w, data); err != nil {
@@ -73,6 +74,7 @@ type LoginPageData struct {
Message string Message string
Error string Error string
CSRFToken string CSRFToken string
BasePath string
} }
// RenderLoginPage рендерит страницу входа // RenderLoginPage рендерит страницу входа

View File

@@ -2,6 +2,7 @@ package serve
import ( import (
"net/http" "net/http"
"strings"
"github.com/direct-dev-ru/linux-command-gpt/config" "github.com/direct-dev-ru/linux-command-gpt/config"
) )
@@ -15,8 +16,8 @@ func AuthMiddleware(next http.HandlerFunc) http.HandlerFunc {
return return
} }
// Исключаем страницу входа и API логина из проверки // Исключаем страницу входа и API логина из проверки (с учетом BasePath)
if r.URL.Path == "/login" || r.URL.Path == "/api/login" || r.URL.Path == "/api/validate-token" { if r.URL.Path == makePath("/login") || r.URL.Path == makePath("/api/login") || r.URL.Path == makePath("/api/validate-token") {
next(w, r) next(w, r)
return return
} }
@@ -31,8 +32,8 @@ func AuthMiddleware(next http.HandlerFunc) http.HandlerFunc {
return return
} }
// Для веб-запросов перенаправляем на страницу входа // Для веб-запросов перенаправляем на страницу входа (с учетом BasePath)
http.Redirect(w, r, "/login", http.StatusSeeOther) http.Redirect(w, r, makePath("/login"), http.StatusSeeOther)
return return
} }
@@ -50,8 +51,8 @@ func CSRFMiddleware(next http.HandlerFunc) http.HandlerFunc {
return return
} }
// Исключаем некоторые API endpoints // Исключаем некоторые API endpoints (с учетом BasePath)
if r.URL.Path == "/api/login" || r.URL.Path == "/api/logout" { if r.URL.Path == makePath("/api/login") || r.URL.Path == makePath("/api/logout") {
next(w, r) next(w, r)
return return
} }
@@ -103,7 +104,8 @@ func CSRFMiddleware(next http.HandlerFunc) http.HandlerFunc {
// isAPIRequest проверяет, является ли запрос API запросом // isAPIRequest проверяет, является ли запрос API запросом
func isAPIRequest(r *http.Request) bool { func isAPIRequest(r *http.Request) bool {
path := r.URL.Path path := r.URL.Path
return len(path) >= 4 && path[:4] == "/api" apiPrefix := makePath("/api")
return strings.HasPrefix(path, apiPrefix)
} }
// RequireAuth обертка для requireAuth из auth.go // RequireAuth обертка для requireAuth из auth.go

View File

@@ -285,7 +285,7 @@ const LoginPageTemplate = `
try { try {
const csrfToken = document.getElementById('csrf_token').value; const csrfToken = document.getElementById('csrf_token').value;
const response = await fetch('/api/login', { const response = await fetch('{{.BasePath}}/api/login', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -302,7 +302,7 @@ const LoginPageTemplate = `
if (data.success) { if (data.success) {
// Успешная авторизация, перенаправляем на главную страницу // Успешная авторизация, перенаправляем на главную страницу
window.location.href = '/'; window.location.href = '{{.BasePath}}/';
} else { } else {
// Ошибка авторизации // Ошибка авторизации
showMessage(data.error || 'Ошибка авторизации', 'error'); showMessage(data.error || 'Ошибка авторизации', 'error');