mirror of
https://github.com/swissmakers/fail2ban-ui.git
synced 2026-04-17 05:53:15 +02:00
Remove old getUserInfo function and old API error-handling
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
// API helper functions for Fail2ban UI
|
// API helpers for Fail2ban UI.
|
||||||
|
"use strict";
|
||||||
|
|
||||||
// Add server parameter to URL
|
// =========================================================================
|
||||||
|
// Server-Scoped Requests
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
|
// Adds the server ID to the URL if a server is selected.
|
||||||
function withServerParam(url) {
|
function withServerParam(url) {
|
||||||
if (!currentServerId) {
|
if (!currentServerId) {
|
||||||
return url;
|
return url;
|
||||||
@@ -8,7 +13,7 @@ function withServerParam(url) {
|
|||||||
return url + (url.indexOf('?') === -1 ? '?' : '&') + 'serverId=' + encodeURIComponent(currentServerId);
|
return url + (url.indexOf('?') === -1 ? '?' : '&') + 'serverId=' + encodeURIComponent(currentServerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get server headers for API requests
|
// Adds the server ID to the headers if a server is selected.
|
||||||
function serverHeaders(headers) {
|
function serverHeaders(headers) {
|
||||||
headers = headers || {};
|
headers = headers || {};
|
||||||
if (currentServerId) {
|
if (currentServerId) {
|
||||||
@@ -16,28 +21,3 @@ function serverHeaders(headers) {
|
|||||||
}
|
}
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auth-aware fetch wrapper that handles 401/403 responses
|
|
||||||
function authFetch(url, options) {
|
|
||||||
options = options || {};
|
|
||||||
// Ensure Accept header for API requests
|
|
||||||
if (!options.headers) {
|
|
||||||
options.headers = {};
|
|
||||||
}
|
|
||||||
if (!options.headers['Accept']) {
|
|
||||||
options.headers['Accept'] = 'application/json';
|
|
||||||
}
|
|
||||||
|
|
||||||
return fetch(url, options).then(function(response) {
|
|
||||||
// Handle authentication errors
|
|
||||||
if (response.status === 401 || response.status === 403) {
|
|
||||||
if (typeof handleAuthError === 'function') {
|
|
||||||
handleAuthError(response);
|
|
||||||
}
|
|
||||||
// Return a rejected promise to stop the chain
|
|
||||||
return Promise.reject(new Error('Authentication required'));
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,18 @@
|
|||||||
// Authentication functions for Fail2ban UI
|
// Auth flow for Fail2ban UI.
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
// Global Variables
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
let authEnabled = false;
|
let authEnabled = false;
|
||||||
let isAuthenticated = false;
|
let isAuthenticated = false;
|
||||||
let currentUser = null;
|
let currentUser = null;
|
||||||
|
|
||||||
// Check authentication status on page load
|
// =========================================================================
|
||||||
|
// Check Authentication Status
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
async function checkAuthStatus() {
|
async function checkAuthStatus() {
|
||||||
// Both login page and main content are hidden by default
|
// Both login page and main content are hidden by default
|
||||||
// We'll show the appropriate one based on authentication status
|
// We'll show the appropriate one based on authentication status
|
||||||
@@ -13,8 +20,6 @@ async function checkAuthStatus() {
|
|||||||
const nav = document.querySelector('nav');
|
const nav = document.querySelector('nav');
|
||||||
const loginPage = document.getElementById('loginPage');
|
const loginPage = document.getElementById('loginPage');
|
||||||
const footer = document.getElementById('footer');
|
const footer = document.getElementById('footer');
|
||||||
|
|
||||||
// Ensure all are hidden initially to prevent flash
|
|
||||||
if (loginPage) {
|
if (loginPage) {
|
||||||
loginPage.classList.add('hidden');
|
loginPage.classList.add('hidden');
|
||||||
loginPage.style.display = 'none';
|
loginPage.style.display = 'none';
|
||||||
@@ -31,21 +36,21 @@ async function checkAuthStatus() {
|
|||||||
footer.classList.add('hidden');
|
footer.classList.add('hidden');
|
||||||
footer.style.display = 'none';
|
footer.style.display = 'none';
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/auth/status', {
|
const response = await fetch('/auth/status', {
|
||||||
headers: serverHeaders()
|
headers: serverHeaders()
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('Failed to check auth status');
|
throw new Error('Failed to check auth status');
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
authEnabled = data.enabled || false;
|
authEnabled = data.enabled || false;
|
||||||
isAuthenticated = data.authenticated || false;
|
isAuthenticated = data.authenticated || false;
|
||||||
const skipLoginPageFlag = data.skipLoginPage || false;
|
const skipLoginPageFlag = data.skipLoginPage || false;
|
||||||
|
|
||||||
if (authEnabled) {
|
if (authEnabled) {
|
||||||
if (isAuthenticated && data.user) {
|
if (isAuthenticated && data.user) {
|
||||||
// Authenticated: show main content, hide login page
|
// Authenticated: show main content, hide login page
|
||||||
@@ -54,7 +59,6 @@ async function checkAuthStatus() {
|
|||||||
} else {
|
} else {
|
||||||
// Not authenticated
|
// Not authenticated
|
||||||
if (skipLoginPageFlag) {
|
if (skipLoginPageFlag) {
|
||||||
// Skip login page: redirect directly to OIDC provider
|
|
||||||
window.location.href = '/auth/login';
|
window.location.href = '/auth/login';
|
||||||
return { enabled: authEnabled, authenticated: false, user: null };
|
return { enabled: authEnabled, authenticated: false, user: null };
|
||||||
} else {
|
} else {
|
||||||
@@ -66,14 +70,13 @@ async function checkAuthStatus() {
|
|||||||
// OIDC not enabled: show main content, hide login page
|
// OIDC not enabled: show main content, hide login page
|
||||||
showMainContent();
|
showMainContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
return { enabled: authEnabled, authenticated: isAuthenticated, user: currentUser };
|
return { enabled: authEnabled, authenticated: isAuthenticated, user: currentUser };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error checking auth status:', error);
|
console.error('Error checking auth status:', error);
|
||||||
// On error, check OIDC status from data attributes
|
|
||||||
const oidcEnabled = document.body.getAttribute('data-oidc-enabled') === 'true';
|
const oidcEnabled = document.body.getAttribute('data-oidc-enabled') === 'true';
|
||||||
const skipLoginPage = document.body.getAttribute('data-skip-login-page') === 'true';
|
const skipLoginPage = document.body.getAttribute('data-skip-login-page') === 'true';
|
||||||
|
|
||||||
if (oidcEnabled) {
|
if (oidcEnabled) {
|
||||||
if (skipLoginPage) {
|
if (skipLoginPage) {
|
||||||
window.location.href = '/auth/login';
|
window.location.href = '/auth/login';
|
||||||
@@ -87,80 +90,47 @@ async function checkAuthStatus() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get current user info
|
// =========================================================================
|
||||||
async function getUserInfo() {
|
// Handle Login and Logout
|
||||||
try {
|
// =========================================================================
|
||||||
const response = await fetch('/auth/user', {
|
|
||||||
headers: serverHeaders()
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
if (response.status === 401) {
|
|
||||||
isAuthenticated = false;
|
|
||||||
currentUser = null;
|
|
||||||
showLoginPage();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
throw new Error('Failed to get user info');
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
if (data.authenticated && data.user) {
|
|
||||||
currentUser = data.user;
|
|
||||||
isAuthenticated = true;
|
|
||||||
return data.user;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error getting user info:', error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle login - redirect to login endpoint with action parameter
|
|
||||||
function handleLogin() {
|
function handleLogin() {
|
||||||
const loginLoading = document.getElementById('loginLoading');
|
const loginLoading = document.getElementById('loginLoading');
|
||||||
const loginError = document.getElementById('loginError');
|
const loginError = document.getElementById('loginError');
|
||||||
const loginErrorText = document.getElementById('loginErrorText');
|
const loginErrorText = document.getElementById('loginErrorText');
|
||||||
const loginButton = event?.target?.closest('button');
|
const loginButton = event?.target?.closest('button');
|
||||||
|
|
||||||
// Show loading state
|
|
||||||
if (loginLoading) loginLoading.classList.remove('hidden');
|
if (loginLoading) loginLoading.classList.remove('hidden');
|
||||||
if (loginButton) {
|
if (loginButton) {
|
||||||
loginButton.disabled = true;
|
loginButton.disabled = true;
|
||||||
loginButton.classList.add('opacity-75', 'cursor-not-allowed');
|
loginButton.classList.add('opacity-75', 'cursor-not-allowed');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hide error if shown
|
|
||||||
if (loginError) {
|
if (loginError) {
|
||||||
loginError.classList.add('hidden');
|
loginError.classList.add('hidden');
|
||||||
if (loginErrorText) loginErrorText.textContent = '';
|
if (loginErrorText) loginErrorText.textContent = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redirect to login endpoint with action=redirect to trigger OIDC redirect
|
|
||||||
window.location.href = '/auth/login?action=redirect';
|
window.location.href = '/auth/login?action=redirect';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle logout - use direct redirect instead of fetch to avoid CORS issues
|
|
||||||
function handleLogout() {
|
function handleLogout() {
|
||||||
// Clear local state
|
// Clear authentication status and redirect to logout endpoint
|
||||||
isAuthenticated = false;
|
isAuthenticated = false;
|
||||||
currentUser = null;
|
currentUser = null;
|
||||||
|
|
||||||
// Direct redirect to logout endpoint (server will handle redirect to provider)
|
|
||||||
// Using window.location.href instead of fetch to avoid CORS issues with redirects
|
|
||||||
window.location.href = '/auth/logout';
|
window.location.href = '/auth/logout';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show login page
|
// =========================================================================
|
||||||
|
// Show Different Application States (Login, Main Content, etc.)
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
function showLoginPage() {
|
function showLoginPage() {
|
||||||
const loginPage = document.getElementById('loginPage');
|
const loginPage = document.getElementById('loginPage');
|
||||||
const mainContent = document.getElementById('mainContent');
|
const mainContent = document.getElementById('mainContent');
|
||||||
const nav = document.querySelector('nav');
|
const nav = document.querySelector('nav');
|
||||||
const footer = document.getElementById('footer');
|
const footer = document.getElementById('footer');
|
||||||
|
|
||||||
// Hide main content, nav, and footer
|
// Hide main content
|
||||||
if (mainContent) {
|
if (mainContent) {
|
||||||
mainContent.style.display = 'none';
|
mainContent.style.display = 'none';
|
||||||
mainContent.classList.add('hidden');
|
mainContent.classList.add('hidden');
|
||||||
@@ -181,7 +151,6 @@ function showLoginPage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show main content (when authenticated or OIDC disabled)
|
|
||||||
function showMainContent() {
|
function showMainContent() {
|
||||||
const loginPage = document.getElementById('loginPage');
|
const loginPage = document.getElementById('loginPage');
|
||||||
const mainContent = document.getElementById('mainContent');
|
const mainContent = document.getElementById('mainContent');
|
||||||
@@ -194,7 +163,7 @@ function showMainContent() {
|
|||||||
loginPage.classList.add('hidden');
|
loginPage.classList.add('hidden');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show main content, nav, and footer
|
// Show main content
|
||||||
if (mainContent) {
|
if (mainContent) {
|
||||||
mainContent.style.display = 'block';
|
mainContent.style.display = 'block';
|
||||||
mainContent.classList.remove('hidden');
|
mainContent.classList.remove('hidden');
|
||||||
@@ -209,30 +178,9 @@ function showMainContent() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Toggle user menu dropdown
|
|
||||||
function toggleUserMenu() {
|
|
||||||
const dropdown = document.getElementById('userMenuDropdown');
|
|
||||||
if (dropdown) {
|
|
||||||
dropdown.classList.toggle('hidden');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close user menu when clicking outside
|
|
||||||
document.addEventListener('click', function(event) {
|
|
||||||
const userMenuButton = document.getElementById('userMenuButton');
|
|
||||||
const userMenuDropdown = document.getElementById('userMenuDropdown');
|
|
||||||
|
|
||||||
if (userMenuButton && userMenuDropdown &&
|
|
||||||
!userMenuButton.contains(event.target) &&
|
|
||||||
!userMenuDropdown.contains(event.target)) {
|
|
||||||
userMenuDropdown.classList.add('hidden');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Show authenticated UI (update header with user info)
|
|
||||||
function showAuthenticatedUI() {
|
function showAuthenticatedUI() {
|
||||||
showMainContent();
|
showMainContent();
|
||||||
|
|
||||||
const userInfoContainer = document.getElementById('userInfoContainer');
|
const userInfoContainer = document.getElementById('userInfoContainer');
|
||||||
const userDisplayName = document.getElementById('userDisplayName');
|
const userDisplayName = document.getElementById('userDisplayName');
|
||||||
const userMenuDisplayName = document.getElementById('userMenuDisplayName');
|
const userMenuDisplayName = document.getElementById('userMenuDisplayName');
|
||||||
@@ -240,20 +188,20 @@ function showAuthenticatedUI() {
|
|||||||
const mobileUserInfoContainer = document.getElementById('mobileUserInfoContainer');
|
const mobileUserInfoContainer = document.getElementById('mobileUserInfoContainer');
|
||||||
const mobileUserDisplayName = document.getElementById('mobileUserDisplayName');
|
const mobileUserDisplayName = document.getElementById('mobileUserDisplayName');
|
||||||
const mobileUserEmail = document.getElementById('mobileUserEmail');
|
const mobileUserEmail = document.getElementById('mobileUserEmail');
|
||||||
|
|
||||||
if (userInfoContainer && currentUser) {
|
if (userInfoContainer && currentUser) {
|
||||||
userInfoContainer.classList.remove('hidden');
|
userInfoContainer.classList.remove('hidden');
|
||||||
|
|
||||||
const displayName = currentUser.name || currentUser.username || currentUser.email;
|
const displayName = currentUser.name || currentUser.username || currentUser.email;
|
||||||
|
|
||||||
if (userDisplayName) {
|
if (userDisplayName) {
|
||||||
userDisplayName.textContent = displayName;
|
userDisplayName.textContent = displayName;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userMenuDisplayName) {
|
if (userMenuDisplayName) {
|
||||||
userMenuDisplayName.textContent = displayName;
|
userMenuDisplayName.textContent = displayName;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userMenuEmail && currentUser.email) {
|
if (userMenuEmail && currentUser.email) {
|
||||||
userMenuEmail.textContent = currentUser.email;
|
userMenuEmail.textContent = currentUser.email;
|
||||||
}
|
}
|
||||||
@@ -262,28 +210,37 @@ function showAuthenticatedUI() {
|
|||||||
// Update mobile menu
|
// Update mobile menu
|
||||||
if (mobileUserInfoContainer && currentUser) {
|
if (mobileUserInfoContainer && currentUser) {
|
||||||
mobileUserInfoContainer.classList.remove('hidden');
|
mobileUserInfoContainer.classList.remove('hidden');
|
||||||
|
|
||||||
const displayName = currentUser.name || currentUser.username || currentUser.email;
|
const displayName = currentUser.name || currentUser.username || currentUser.email;
|
||||||
|
|
||||||
if (mobileUserDisplayName) {
|
if (mobileUserDisplayName) {
|
||||||
mobileUserDisplayName.textContent = displayName;
|
mobileUserDisplayName.textContent = displayName;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mobileUserEmail && currentUser.email) {
|
if (mobileUserEmail && currentUser.email) {
|
||||||
mobileUserEmail.textContent = currentUser.email;
|
mobileUserEmail.textContent = currentUser.email;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle 401/403 responses from API
|
// =========================================================================
|
||||||
function handleAuthError(response) {
|
// Helper Functions
|
||||||
if (response.status === 401 || response.status === 403) {
|
// =========================================================================
|
||||||
if (authEnabled) {
|
|
||||||
isAuthenticated = false;
|
function toggleUserMenu() {
|
||||||
currentUser = null;
|
const dropdown = document.getElementById('userMenuDropdown');
|
||||||
showLoginPage();
|
if (dropdown) {
|
||||||
return true;
|
dropdown.classList.toggle('hidden');
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
document.addEventListener('click', function(event) {
|
||||||
|
const userMenuButton = document.getElementById('userMenuButton');
|
||||||
|
const userMenuDropdown = document.getElementById('userMenuDropdown');
|
||||||
|
|
||||||
|
if (userMenuButton && userMenuDropdown &&
|
||||||
|
!userMenuButton.contains(event.target) &&
|
||||||
|
!userMenuDropdown.contains(event.target)) {
|
||||||
|
userMenuDropdown.classList.add('hidden');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user