// Global variable to store the JWT token let authToken = localStorage.getItem("auth_token"); $(document).ready(function () { // Initialize tooltips $('[data-bs-toggle="tooltip"]').tooltip(); // Set up AJAX defaults to include auth token $.ajaxSetup({ beforeSend: function (xhr, settings) { // Don't add auth header for login request if (settings.url === "/api/auth/login") { return; } if (authToken) { xhr.setRequestHeader("Authorization", "Bearer " + authToken); } }, error: function (xhr, status, error) { // If we get a 401, redirect to login if (xhr.status === 401) { localStorage.removeItem("auth_token"); window.location.href = "/login"; return; } handleAjaxError(xhr, status, error); }, }); // Handle login form submission $("#loginForm").on("submit", function (e) { e.preventDefault(); const secretKey = $("#secret_key").val(); $("#loginError").hide(); $.ajax({ url: "/api/auth/login", method: "POST", contentType: "application/json", data: JSON.stringify({ secret_key: secretKey }), success: function (response) { if (response && response.token) { // Store token and redirect localStorage.setItem("auth_token", response.token); authToken = response.token; window.location.href = "/dashboard"; } else { $("#loginError").text("Invalid response from server").show(); } }, error: function (xhr) { console.error("Login error:", xhr); if (xhr.responseJSON && xhr.responseJSON.error) { $("#loginError").text(xhr.responseJSON.error).show(); } else { $("#loginError").text("Login failed. Please try again.").show(); } $("#secret_key").val("").focus(); }, }); }); // Only load dashboard data if we're on the dashboard page if (window.location.pathname === "/dashboard") { if (!authToken) { window.location.href = "/login"; return; } // Load initial data loadProjects(); loadAPIKeys(); loadLogs(); checkHealth(); // Set up periodic health check setInterval(checkHealth, 30000); } // Project management $("#addProjectForm").on("submit", function (e) { e.preventDefault(); const projectData = { name: $("#projectName").val(), jenkinsJob: $("#jenkinsJob").val(), giteaRepo: $("#giteaRepo").val(), }; $.ajax({ url: "/api/projects", method: "POST", contentType: "application/json", data: JSON.stringify(projectData), success: function () { $("#addProjectModal").modal("hide"); loadProjects(); }, error: handleAjaxError, }); }); // API key management $("#generateKeyForm").on("submit", function (e) { e.preventDefault(); $.ajax({ url: "/api/keys", method: "POST", contentType: "application/json", data: JSON.stringify({ description: $("#keyDescription").val() }), success: function () { $("#generateKeyModal").modal("hide"); loadAPIKeys(); }, error: handleAjaxError, }); }); // Log querying $("#logQueryForm").on("submit", function (e) { e.preventDefault(); loadLogs({ startTime: $("#startTime").val(), endTime: $("#endTime").val(), level: $("#logLevel").val(), query: $("#logQuery").val(), }); }); }); function loadProjects() { $.get("/api/projects") .done(function (data) { const tbody = $("#projectsTable tbody"); tbody.empty(); data.projects.forEach(function (project) { tbody.append(`
${escapeHtml(
key.value
)}