1
1
import axios from "axios" ;
2
2
import { getAnalytics , sendSDKLogsToBackend } from "./utils" ;
3
+ import { getUserInformation } from "./api/user/info" ;
3
4
4
5
export enum HTTP_REQUEST_ERROR {
5
6
SESSION_EXPIRED ,
@@ -230,4 +231,59 @@ const sendAuthAnalytics = (eventName: string, payload: Record<string, unknown>,
230
231
version
231
232
) ;
232
233
} ) ;
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
+
0 commit comments