Skip to content

Commit ebad81f

Browse files
committed
using cache value
1 parent e8ef711 commit ebad81f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/shared/mobile.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ export interface MobileSession {
33
s?: string
44
}
55

6+
// In-memory cache: survives SPA navigations without localStorage
7+
let cachedSession: MobileSession | null | undefined
8+
69
// Helper that parses the state param from the OAuth callback (same pattern as extractRedirectToFromSearchParameters)
710
function extractMobileDataFromState(): { isMobileFlow?: boolean; mobileUserId?: string; mobileSessionId?: string } | null {
811
try {
@@ -24,20 +27,26 @@ function extractMobileDataFromState(): { isMobileFlow?: boolean; mobileUserId?:
2427
}
2528

2629
export function getMobileSession(): MobileSession | null {
30+
if (cachedSession !== undefined) {
31+
return cachedSession
32+
}
33+
2734
const params = new URLSearchParams(window.location.search)
2835

2936
// On /auth/mobile, read from URL params
3037
if (window.location.pathname.startsWith('/auth/mobile')) {
31-
return { u: params.get('u') ?? undefined, s: params.get('s') ?? undefined }
38+
cachedSession = { u: params.get('u') ?? undefined, s: params.get('s') ?? undefined }
39+
return cachedSession
3240
}
3341

3442
// On callback, extract from OAuth state param
3543
const stateData = extractMobileDataFromState()
3644
if (stateData?.isMobileFlow) {
37-
return {
45+
cachedSession = {
3846
u: stateData.mobileUserId ?? undefined,
3947
s: stateData.mobileSessionId ?? undefined
4048
}
49+
return cachedSession
4150
}
4251

4352
return null

0 commit comments

Comments
 (0)