Add check for expired refresh or session token that exists
This commit is contained in:
@@ -131,10 +131,13 @@ export function useConnection({
|
|||||||
return tokens.access_token;
|
return tokens.access_token;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Token refresh failed:", error);
|
console.error("Token refresh failed:", error);
|
||||||
// Clear tokens and redirect to home
|
// If refresh token is expired/invalid (401) or any other error,
|
||||||
|
// clear tokens and redirect to home to trigger re-authentication
|
||||||
sessionStorage.removeItem(SESSION_KEYS.ACCESS_TOKEN);
|
sessionStorage.removeItem(SESSION_KEYS.ACCESS_TOKEN);
|
||||||
sessionStorage.removeItem(SESSION_KEYS.REFRESH_TOKEN);
|
sessionStorage.removeItem(SESSION_KEYS.REFRESH_TOKEN);
|
||||||
window.location.href = "/";
|
sessionStorage.setItem(SESSION_KEYS.SERVER_URL, sseUrl);
|
||||||
|
const redirectUrl = await startOAuthFlow(sseUrl);
|
||||||
|
window.location.href = redirectUrl;
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -178,12 +181,27 @@ export function useConnection({
|
|||||||
fetch: async (url, init) => {
|
fetch: async (url, init) => {
|
||||||
const response = await fetch(url, { ...init, headers });
|
const response = await fetch(url, { ...init, headers });
|
||||||
|
|
||||||
if (response.status === 401 && sessionStorage.getItem(SESSION_KEYS.REFRESH_TOKEN)) {
|
if (response.status === 401) {
|
||||||
// Try to refresh the token
|
// First try to refresh if we have a refresh token
|
||||||
const newAccessToken = await handleTokenRefresh();
|
if (sessionStorage.getItem(SESSION_KEYS.REFRESH_TOKEN)) {
|
||||||
headers["Authorization"] = `Bearer ${newAccessToken}`;
|
try {
|
||||||
// Retry the request with new token
|
const newAccessToken = await handleTokenRefresh();
|
||||||
return fetch(url, { ...init, headers });
|
headers["Authorization"] = `Bearer ${newAccessToken}`;
|
||||||
|
// Retry the request with new token
|
||||||
|
return fetch(url, { ...init, headers });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Token refresh failed:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we have an access token but refresh failed or wasn't available,
|
||||||
|
// we need to re-authenticate since the token is invalid
|
||||||
|
if (sessionStorage.getItem(SESSION_KEYS.ACCESS_TOKEN)) {
|
||||||
|
sessionStorage.setItem(SESSION_KEYS.SERVER_URL, sseUrl);
|
||||||
|
const redirectUrl = await startOAuthFlow(sseUrl);
|
||||||
|
window.location.href = redirectUrl;
|
||||||
|
return new Response(); // This won't actually be used due to redirect
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
|
|||||||
Reference in New Issue
Block a user