From b7b42111bcc48a1f3fd9ea597eacf3630d217bf0 Mon Sep 17 00:00:00 2001 From: Favour Ohans Date: Tue, 9 Jul 2024 17:46:09 +0100 Subject: [PATCH] Log desynced session --- v2/src/components/httpNetworking.ts | 58 ++++++++++++++++++++++++++++- v2/src/theme/Layout/index.js | 4 +- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/v2/src/components/httpNetworking.ts b/v2/src/components/httpNetworking.ts index e829473af..925bab90f 100644 --- a/v2/src/components/httpNetworking.ts +++ b/v2/src/components/httpNetworking.ts @@ -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, @@ -230,4 +231,59 @@ const sendAuthAnalytics = (eventName: string, payload: Record, version ); }); -}; \ No newline at end of file +}; + +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; + }; + } + \ No newline at end of file diff --git a/v2/src/theme/Layout/index.js b/v2/src/theme/Layout/index.js index b5b18341f..6cf8e0a68 100644 --- a/v2/src/theme/Layout/index.js +++ b/v2/src/theme/Layout/index.js @@ -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"; @@ -72,6 +72,8 @@ if (typeof window !== 'undefined') { } } }); + checkForDesyncedSession(); + historyPushStateOverride(checkForDesyncedSession); } function OriginalLayout(props) {