Skip to content

Commit b959f01

Browse files
Merge pull request #811 from supertokens/chore/desynced-session-state-logs
Chore: Log desynced session
2 parents eb04cf4 + b7b4211 commit b959f01

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

v2/src/components/httpNetworking.ts

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import axios from "axios";
22
import { getAnalytics, sendSDKLogsToBackend } from "./utils";
3+
import { getUserInformation } from "./api/user/info";
34

45
export enum HTTP_REQUEST_ERROR {
56
SESSION_EXPIRED,
@@ -230,4 +231,59 @@ const sendAuthAnalytics = (eventName: string, payload: Record<string, unknown>,
230231
version
231232
);
232233
});
233-
};
234+
};
235+
236+
function getCookieValue(cookieName: string) {
237+
const cookies = document.cookie;
238+
const cookieArray = cookies.split(';');
239+
for (let i = 0; i < cookieArray.length; i++) {
240+
const cookie = cookieArray[i].trim();
241+
if (cookie.startsWith(cookieName + '=')) {
242+
return cookie.substring(cookieName.length + 1);
243+
}
244+
}
245+
return null;
246+
}
247+
248+
export async function checkForDesyncedSession() {
249+
const EVENT_NAME = 'desynced_session_state';
250+
try {
251+
const didFrontTokenExistBeforeAPICall = cookieExists('sFrontToken');
252+
await getUserInformation();
253+
const doesFrontendTokenExistAfterAPICall = cookieExists('sFrontToken');
254+
255+
if (!doesFrontendTokenExistAfterAPICall) {
256+
const payload = {
257+
didFrontTokenExistBeforeAPICall,
258+
stLastAccessTokenUpdate: getCookieValue('st-last-access-token-update'),
259+
};
260+
261+
getAnalytics().then((stAnalytics: any) => {
262+
if (stAnalytics === undefined) {
263+
console.log('mocked event send:', EVENT_NAME, 'v1', payload);
264+
return;
265+
}
266+
stAnalytics.sendEvent(
267+
EVENT_NAME,
268+
{
269+
type: EVENT_NAME,
270+
...payload,
271+
},
272+
'v1'
273+
);
274+
});
275+
}
276+
} catch (e) {
277+
// ignore
278+
}
279+
}
280+
281+
export function historyPushStateOverride(onPush: () => void) {
282+
const originalPushState = history.pushState;
283+
history.pushState = function (...args) {
284+
const result = originalPushState.apply(this, args);
285+
onPush();
286+
return result;
287+
};
288+
}
289+

v2/src/theme/Layout/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { useLocation } from '@docusaurus/router';
1919
import './styles.css';
2020
import supertokens from "supertokens-website";
2121
import {overrideConsoleImplementation,saveSDKLogsConsoleOverride, sendSDKLogsToBackend} from '../../components/utils'
22-
import {cookieExists} from '../../components/httpNetworking'
22+
import {checkForDesyncedSession, cookieExists, historyPushStateOverride} from '../../components/httpNetworking'
2323
import styles from "./styles.module.css";
2424

2525

@@ -72,6 +72,8 @@ if (typeof window !== 'undefined') {
7272
}
7373
}
7474
});
75+
checkForDesyncedSession();
76+
historyPushStateOverride(checkForDesyncedSession);
7577
}
7678

7779
function OriginalLayout(props) {

0 commit comments

Comments
 (0)