Skip to content

Commit

Permalink
Merge pull request #812 from supertokens/chore/401-desynced
Browse files Browse the repository at this point in the history
chore: Handle 401 error in desynced session
  • Loading branch information
rishabhpoddar authored Jul 11, 2024
2 parents 9a79462 + 00f0608 commit 2a3d6ef
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions v2/src/components/httpNetworking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,18 @@ function getCookieValue(cookieName: string) {

export async function checkForDesyncedSession() {
const EVENT_NAME = 'desynced_session_state';
const didFrontTokenExistBeforeAPICall = cookieExists("sFrontToken");

try {
const didFrontTokenExistBeforeAPICall = cookieExists('sFrontToken');
await getUserInformation();
const doesFrontendTokenExistAfterAPICall = cookieExists('sFrontToken');

if (!doesFrontendTokenExistAfterAPICall) {
const payload = {
didFrontTokenExistBeforeAPICall,
stLastAccessTokenUpdate: getCookieValue('st-last-access-token-update'),
};

getAnalytics().then((stAnalytics: any) => {
if (stAnalytics === undefined) {
await getUserInformation();
const doesFrontendTokenExistAfterAPICall = cookieExists("sFrontToken");
if (!doesFrontendTokenExistAfterAPICall) {
const payload = {
didFrontTokenExistBeforeAPICall,
stLastAccessTokenUpdate: getCookieValue("st-last-access-token-update"),
};
getAnalytics().then((stAnalytics: any) => {
if (stAnalytics === undefined) {
console.log('mocked event send:', EVENT_NAME, 'v1', payload);
return;
}
Expand All @@ -273,8 +272,34 @@ function getCookieValue(cookieName: string) {
);
});
}
} catch (e) {
// ignore
} catch (e:any) {
if (
"response" in e &&
e.response.status === 401 &&
e.response.data &&
e.response.data.message === "try refresh token"
) {
if (!cookieExists("sFrontToken")) {
const payload = {
didFrontTokenExistBeforeAPICall,
stLastAccessTokenUpdate: getCookieValue("st-last-access-token-update"),
};
getAnalytics().then((stAnalytics: any) => {
if (stAnalytics === undefined) {
console.log("mocked event send:", EVENT_NAME, "v1", payload);
return;
}
stAnalytics.sendEvent(
EVENT_NAME,
{
type: EVENT_NAME,
...payload
},
"v1"
);
});
}
}
}
}

Expand Down

0 comments on commit 2a3d6ef

Please sign in to comment.