Skip to content

Commit f6baa1b

Browse files
committed
refac
1 parent 5134e5e commit f6baa1b

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

src/routes/+layout.svelte

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,20 @@
810810
});
811811
};
812812
813+
const isAuthFailureResponse = async (response) => {
814+
try {
815+
const data = await response.clone().json();
816+
const detail = data?.detail;
817+
return (
818+
detail === '401 Unauthorized' ||
819+
detail === 'Not authenticated' ||
820+
detail === 'Your session has expired or the token is invalid. Please sign in again.'
821+
);
822+
} catch {
823+
return true;
824+
}
825+
};
826+
813827
const checkTokenExpiry = async () => {
814828
const exp = $user?.expires_at; // token expiry time in unix timestamp
815829
const now = Math.floor(Date.now() / 1000); // current time in unix timestamp
@@ -939,7 +953,8 @@
939953
if (
940954
response.status === 401 &&
941955
localStorage.token &&
942-
isAuthenticatedBackendFetch(input, init)
956+
isAuthenticatedBackendFetch(input, init) &&
957+
(await isAuthFailureResponse(response))
943958
) {
944959
redirectToAuthAfterUnauthorized();
945960
}

0 commit comments

Comments
 (0)