баг-фикс 30/34
This commit is contained in:
@@ -1,23 +1,38 @@
|
||||
let refreshPromise = null;
|
||||
|
||||
const AUTH_KEYS = ['token', 'role', 'departmentId', 'userId'];
|
||||
import {
|
||||
clearAuthState,
|
||||
fetchWithAuth,
|
||||
getAccessToken,
|
||||
getAuthState,
|
||||
logoutSession,
|
||||
refreshAccessToken,
|
||||
restoreSession,
|
||||
storeAuthState
|
||||
} from '../../auth-session.js';
|
||||
|
||||
export function getToken() {
|
||||
return localStorage.getItem('token');
|
||||
return getAccessToken();
|
||||
}
|
||||
|
||||
export function getSession() {
|
||||
return getAuthState();
|
||||
}
|
||||
|
||||
export async function initializeSession() {
|
||||
return restoreSession();
|
||||
}
|
||||
|
||||
export function isAuthenticatedAsAdmin() {
|
||||
const role = localStorage.getItem('role');
|
||||
const role = getAuthState()?.role;
|
||||
return getToken() && role === 'ADMIN';
|
||||
}
|
||||
|
||||
export function isAuthenticatedAsRole(expectedRole) {
|
||||
const role = localStorage.getItem('role');
|
||||
const role = getAuthState()?.role;
|
||||
return getToken() && role === expectedRole;
|
||||
}
|
||||
|
||||
export function isAuthenticatedAsAny(roles) {
|
||||
const role = localStorage.getItem('role');
|
||||
const role = getAuthState()?.role;
|
||||
return getToken() && roles.includes(role);
|
||||
}
|
||||
|
||||
@@ -44,7 +59,7 @@ export async function apiFetch(endpoint, method = 'GET', body = null, retryOnUna
|
||||
options.body = JSON.stringify(body);
|
||||
}
|
||||
|
||||
const response = await fetch(endpoint, options);
|
||||
const response = await fetchWithAuth(endpoint, options);
|
||||
|
||||
let data;
|
||||
try {
|
||||
@@ -54,12 +69,8 @@ export async function apiFetch(endpoint, method = 'GET', body = null, retryOnUna
|
||||
}
|
||||
|
||||
if (response.status === 401 && retryOnUnauthorized) {
|
||||
const refreshed = await refreshAccessToken();
|
||||
if (refreshed) {
|
||||
return apiFetch(endpoint, method, body, false);
|
||||
}
|
||||
clearAuthState();
|
||||
window.location.href = '/';
|
||||
window.location.replace('/');
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -72,28 +83,7 @@ export async function apiFetch(endpoint, method = 'GET', body = null, retryOnUna
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function refreshAccessToken() {
|
||||
if (refreshPromise) {
|
||||
return refreshPromise;
|
||||
}
|
||||
|
||||
refreshPromise = (async () => {
|
||||
const response = await fetch('/api/auth/refresh', {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin'
|
||||
});
|
||||
const data = await response.json().catch(() => null);
|
||||
if (!response.ok || !data?.token) {
|
||||
return false;
|
||||
}
|
||||
storeAuthState(data);
|
||||
return true;
|
||||
})().finally(() => {
|
||||
refreshPromise = null;
|
||||
});
|
||||
|
||||
return refreshPromise;
|
||||
}
|
||||
export { clearAuthState, refreshAccessToken, storeAuthState };
|
||||
|
||||
const cache = new Map();
|
||||
|
||||
@@ -139,32 +129,8 @@ export function clearCache() {
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
try {
|
||||
await fetch('/api/auth/logout', {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin'
|
||||
});
|
||||
} catch (e) {
|
||||
console.warn('Не удалось завершить серверную сессию:', e.message);
|
||||
} finally {
|
||||
clearAuthState();
|
||||
clearCache();
|
||||
}
|
||||
}
|
||||
|
||||
export function storeAuthState(data) {
|
||||
if (data.token) localStorage.setItem('token', data.token);
|
||||
if (data.role) localStorage.setItem('role', data.role);
|
||||
if (data.departmentId !== undefined && data.departmentId !== null) {
|
||||
localStorage.setItem('departmentId', data.departmentId);
|
||||
}
|
||||
if (data.userId !== undefined && data.userId !== null) {
|
||||
localStorage.setItem('userId', data.userId);
|
||||
}
|
||||
}
|
||||
|
||||
export function clearAuthState() {
|
||||
AUTH_KEYS.forEach(key => localStorage.removeItem(key));
|
||||
await logoutSession();
|
||||
clearCache();
|
||||
}
|
||||
|
||||
export const api = {
|
||||
|
||||
Reference in New Issue
Block a user