This commit is contained in:
HotSwapp
2025-08-08 19:06:39 -05:00
parent b257a06787
commit 04edc636f8
12 changed files with 1824 additions and 52 deletions

View File

@@ -78,10 +78,18 @@
<script>
document.addEventListener('DOMContentLoaded', function() {
// Check if already logged in
// Check for logout reason
const logoutReason = sessionStorage.getItem('logout_reason');
if (logoutReason) {
showAlert(logoutReason, 'warning');
sessionStorage.removeItem('logout_reason');
}
// Check if already logged in with valid token
const token = localStorage.getItem('auth_token');
if (token) {
window.location.href = '/customers';
// Verify token is still valid before redirecting
checkTokenAndRedirect(token);
return;
}
@@ -150,6 +158,28 @@
document.getElementById('username').focus();
});
async function checkTokenAndRedirect(token) {
try {
const response = await fetch('/api/auth/me', {
headers: {
'Authorization': `Bearer ${token}`
}
});
if (response.ok) {
// Token is valid, redirect to customers page
window.location.href = '/customers';
} else {
// Token is invalid, remove it
localStorage.removeItem('auth_token');
}
} catch (error) {
// Error checking token, remove it
localStorage.removeItem('auth_token');
console.error('Error checking token:', error);
}
}
function showAlert(message, type = 'info') {
// Remove existing alerts
const existingAlerts = document.querySelectorAll('.alert');