mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Fix refresh problems on background outdated tabs (#8850)
This commit is contained in:
@@ -104,7 +104,7 @@ api.interceptors.response.use(onResponse, onError);
|
||||
export default api;
|
||||
|
||||
export function getToken(): string | null {
|
||||
return api.defaults.headers?.['Authorization']?.split(' ')[1] || null;
|
||||
return api.defaults.headers.common['Authorization']?.split(' ')[1] || null;
|
||||
}
|
||||
|
||||
export function addTokenToURL(url: string, token?: string): string {
|
||||
|
||||
@@ -20,12 +20,8 @@ export async function login(credentials: LoginCredentials): Promise<void> {
|
||||
|
||||
const accessToken = response.data.data.access_token;
|
||||
|
||||
if (!api.defaults.headers) {
|
||||
api.defaults.headers = {};
|
||||
}
|
||||
|
||||
// Add the header to the API handler for every request
|
||||
api.defaults.headers['Authorization'] = `Bearer ${accessToken}`;
|
||||
api.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`;
|
||||
|
||||
// Refresh the token 10 seconds before the access token expires. This means the user will stay
|
||||
// logged in without any noticable hickups or delays
|
||||
@@ -77,7 +73,7 @@ export async function refresh({ navigate }: LogoutOptions = { navigate: true }):
|
||||
// Allow refresh during initial page load
|
||||
if (firstRefresh) firstRefresh = false;
|
||||
// Skip if not logged in
|
||||
else if (!api.defaults.headers?.['Authorization']) return;
|
||||
else if (!api.defaults.headers.common['Authorization']) return;
|
||||
|
||||
// Prevent concurrent refreshes
|
||||
if (isRefreshing) return;
|
||||
@@ -92,12 +88,18 @@ export async function refresh({ navigate }: LogoutOptions = { navigate: true }):
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await api.post<any>('/auth/refresh', { headers: { Authorization: undefined } });
|
||||
const response = await api.post<any>('/auth/refresh', undefined, {
|
||||
transformRequest(data, headers) {
|
||||
// This seems wrongly typed in Axios itself..
|
||||
delete (headers?.common as unknown as Record<string, string>)?.['Authorization'];
|
||||
return data;
|
||||
},
|
||||
});
|
||||
|
||||
const accessToken = response.data.data.access_token;
|
||||
|
||||
// Add the header to the API handler for every request
|
||||
api.defaults.headers['Authorization'] = `Bearer ${accessToken}`;
|
||||
api.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`;
|
||||
|
||||
// Refresh the token 10 seconds before the access token expires. This means the user will stay
|
||||
// logged in without any notable hiccups or delays
|
||||
@@ -141,7 +143,7 @@ export async function logout(optionsRaw: LogoutOptions = {}): Promise<void> {
|
||||
reason: LogoutReason.SIGN_OUT,
|
||||
};
|
||||
|
||||
delete api.defaults.headers?.Authorization;
|
||||
delete api.defaults.headers.common['Authorization'];
|
||||
|
||||
clearTimeout(refreshTimeout);
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ export default defineComponent({
|
||||
const url = getRootPath() + endpoint;
|
||||
|
||||
let params: Record<string, unknown> = {
|
||||
access_token: api.defaults.headers?.Authorization.substring(7),
|
||||
access_token: api.defaults.headers.common['Authorization'].substring(7),
|
||||
export: format.value || 'json',
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user