Skip to content

Commit

Permalink
Merge pull request #811 from supertokens/chore/desynced-session-state…
Browse files Browse the repository at this point in the history
…-logs

Chore: Log desynced session
  • Loading branch information
rishabhpoddar authored Jul 9, 2024
2 parents eb04cf4 + b7b4211 commit b959f01
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
58 changes: 57 additions & 1 deletion v2/src/components/httpNetworking.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from "axios";
import { getAnalytics, sendSDKLogsToBackend } from "./utils";
import { getUserInformation } from "./api/user/info";

export enum HTTP_REQUEST_ERROR {
SESSION_EXPIRED,
Expand Down Expand Up @@ -230,4 +231,59 @@ const sendAuthAnalytics = (eventName: string, payload: Record<string, unknown>,
version
);
});
};
};

function getCookieValue(cookieName: string) {
const cookies = document.cookie;
const cookieArray = cookies.split(';');
for (let i = 0; i < cookieArray.length; i++) {
const cookie = cookieArray[i].trim();
if (cookie.startsWith(cookieName + '=')) {
return cookie.substring(cookieName.length + 1);
}
}
return null;
}

export async function checkForDesyncedSession() {
const EVENT_NAME = 'desynced_session_state';
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) {
console.log('mocked event send:', EVENT_NAME, 'v1', payload);
return;
}
stAnalytics.sendEvent(
EVENT_NAME,
{
type: EVENT_NAME,
...payload,
},
'v1'
);
});
}
} catch (e) {
// ignore
}
}

export function historyPushStateOverride(onPush: () => void) {
const originalPushState = history.pushState;
history.pushState = function (...args) {
const result = originalPushState.apply(this, args);
onPush();
return result;
};
}

4 changes: 3 additions & 1 deletion v2/src/theme/Layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useLocation } from '@docusaurus/router';
import './styles.css';
import supertokens from "supertokens-website";
import {overrideConsoleImplementation,saveSDKLogsConsoleOverride, sendSDKLogsToBackend} from '../../components/utils'
import {cookieExists} from '../../components/httpNetworking'
import {checkForDesyncedSession, cookieExists, historyPushStateOverride} from '../../components/httpNetworking'
import styles from "./styles.module.css";


Expand Down Expand Up @@ -72,6 +72,8 @@ if (typeof window !== 'undefined') {
}
}
});
checkForDesyncedSession();
historyPushStateOverride(checkForDesyncedSession);
}

function OriginalLayout(props) {
Expand Down

0 comments on commit b959f01

Please sign in to comment.