fix menu tax in data and fix session when multiple user login
This commit is contained in:
@@ -171,16 +171,23 @@ class ApiTokenManager {
|
||||
try {
|
||||
const response = await fetch(url, mergedOptions);
|
||||
|
||||
// If unauthorized, try to generate new token
|
||||
if (response.status === 401 && this.hasToken()) {
|
||||
console.log('Token expired, generating new token...');
|
||||
const newToken = await this.generateToken();
|
||||
|
||||
if (newToken) {
|
||||
// Retry request with new token
|
||||
mergedOptions.headers.Authorization = `Bearer ${newToken}`;
|
||||
return fetch(url, mergedOptions);
|
||||
// If unauthorized, check if it's a session issue
|
||||
if (response.status === 401) {
|
||||
if (this.hasToken()) {
|
||||
console.log('Token expired, generating new token...');
|
||||
const newToken = await this.generateToken();
|
||||
|
||||
if (newToken) {
|
||||
// Retry request with new token
|
||||
mergedOptions.headers.Authorization = `Bearer ${newToken}`;
|
||||
return fetch(url, mergedOptions);
|
||||
}
|
||||
}
|
||||
|
||||
// If still 401, it might be a session issue
|
||||
console.log('Session invalid, redirecting to login...');
|
||||
this.handleSessionInvalid();
|
||||
return response;
|
||||
}
|
||||
|
||||
return response;
|
||||
@@ -189,6 +196,33 @@ class ApiTokenManager {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
handleSessionInvalid() {
|
||||
// Show notification
|
||||
this.showNotification('Session Anda telah berakhir. Silakan login ulang.', 'warning');
|
||||
|
||||
// Redirect to login after 3 seconds
|
||||
setTimeout(() => {
|
||||
window.location.href = '/login';
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
showNotification(message, type = 'info') {
|
||||
// Check if notification library exists
|
||||
if (typeof toastr !== 'undefined') {
|
||||
toastr[type](message);
|
||||
} else if (typeof Swal !== 'undefined') {
|
||||
Swal.fire({
|
||||
title: 'Peringatan',
|
||||
text: message,
|
||||
icon: type,
|
||||
confirmButtonText: 'OK'
|
||||
});
|
||||
} else {
|
||||
// Fallback to alert
|
||||
alert(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Export singleton instance
|
||||
|
||||
Reference in New Issue
Block a user