v2
This commit is contained in:
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user