@payez/next-mvp 3.9.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/dist/api/auth-handler.d.ts +1 -2
  2. package/dist/api/auth-handler.js +9 -9
  3. package/dist/api-handlers/account/change-password.js +110 -112
  4. package/dist/api-handlers/admin/analytics.d.ts +19 -20
  5. package/dist/api-handlers/admin/analytics.js +378 -379
  6. package/dist/api-handlers/admin/audit.d.ts +19 -20
  7. package/dist/api-handlers/admin/audit.js +213 -214
  8. package/dist/api-handlers/admin/index.d.ts +21 -22
  9. package/dist/api-handlers/admin/index.js +42 -43
  10. package/dist/api-handlers/admin/redis-sessions.d.ts +35 -36
  11. package/dist/api-handlers/admin/redis-sessions.js +203 -204
  12. package/dist/api-handlers/admin/sessions.d.ts +20 -21
  13. package/dist/api-handlers/admin/sessions.js +283 -284
  14. package/dist/api-handlers/admin/site-logs.d.ts +45 -46
  15. package/dist/api-handlers/admin/site-logs.js +317 -318
  16. package/dist/api-handlers/admin/stats.d.ts +20 -21
  17. package/dist/api-handlers/admin/stats.js +239 -240
  18. package/dist/api-handlers/admin/users.d.ts +19 -20
  19. package/dist/api-handlers/admin/users.js +221 -222
  20. package/dist/api-handlers/admin/vibe-data.d.ts +79 -80
  21. package/dist/api-handlers/admin/vibe-data.js +267 -268
  22. package/dist/api-handlers/auth/refresh.js +633 -635
  23. package/dist/api-handlers/auth/signout.js +186 -187
  24. package/dist/api-handlers/auth/status.js +4 -7
  25. package/dist/api-handlers/auth/update-session.d.ts +1 -1
  26. package/dist/api-handlers/auth/update-session.js +12 -14
  27. package/dist/api-handlers/auth/verify-code.d.ts +43 -43
  28. package/dist/api-handlers/auth/verify-code.js +90 -94
  29. package/dist/api-handlers/session/viability.js +114 -146
  30. package/dist/api-handlers/test/force-expire.js +59 -65
  31. package/dist/auth/auth-decision.js +182 -182
  32. package/dist/auth/better-auth.d.ts +3 -6
  33. package/dist/auth/better-auth.js +3 -6
  34. package/dist/auth/route-config.js +2 -2
  35. package/dist/auth/utils/token-utils.d.ts +83 -84
  36. package/dist/auth/utils/token-utils.js +218 -219
  37. package/dist/client/AuthContext.js +115 -112
  38. package/dist/client/better-auth-client.d.ts +1020 -961
  39. package/dist/client/better-auth-client.js +54 -7
  40. package/dist/client/fetch-with-auth.js +2 -2
  41. package/dist/components/SessionSync.js +121 -119
  42. package/dist/components/account/MobileNavDrawer.js +64 -64
  43. package/dist/components/account/UserAvatarMenu.js +91 -88
  44. package/dist/components/admin/VibeAdminLayout.js +71 -69
  45. package/dist/hooks/useAuth.js +9 -7
  46. package/dist/hooks/useAuthSettings.js +93 -93
  47. package/dist/hooks/useAvailableProviders.d.ts +43 -45
  48. package/dist/hooks/useAvailableProviders.js +112 -108
  49. package/dist/hooks/useSessionExpiration.d.ts +2 -3
  50. package/dist/hooks/useSessionExpiration.js +2 -2
  51. package/dist/hooks/useViabilitySession.js +3 -2
  52. package/dist/index.js +4 -6
  53. package/dist/lib/app-slug.d.ts +95 -95
  54. package/dist/lib/app-slug.js +172 -172
  55. package/dist/lib/standardized-client-api.js +10 -5
  56. package/dist/lib/startup-init.js +21 -25
  57. package/dist/lib/test-aware-get-token.js +86 -81
  58. package/dist/lib/token-lifecycle.d.ts +78 -52
  59. package/dist/lib/token-lifecycle.js +360 -398
  60. package/dist/pages/admin-login/page.js +73 -83
  61. package/dist/pages/client-admin/ClientSiteAdminPage.js +179 -177
  62. package/dist/pages/login/page.js +202 -211
  63. package/dist/pages/showcase/ShowcasePage.js +142 -140
  64. package/dist/pages/test-env/EmergencyLogoutPage.js +99 -98
  65. package/dist/pages/test-env/JwtInspectPage.js +116 -114
  66. package/dist/pages/test-env/RefreshTokenPage.js +4 -2
  67. package/dist/pages/test-env/TestEnvPage.js +51 -49
  68. package/dist/pages/verify-code/page.js +412 -408
  69. package/dist/routes/auth/logout.d.ts +31 -31
  70. package/dist/routes/auth/logout.js +98 -113
  71. package/dist/routes/auth/nextauth.d.ts +14 -11
  72. package/dist/routes/auth/nextauth.js +25 -57
  73. package/dist/routes/auth/session.js +157 -179
  74. package/dist/routes/auth/viability.js +190 -201
  75. package/dist/server/auth.d.ts +50 -0
  76. package/dist/server/auth.js +62 -0
  77. package/dist/stores/authStore.js +19 -23
  78. package/dist/utils/logout.js +5 -5
  79. package/package.json +1 -3
  80. package/src/api/auth-handler.ts +550 -549
  81. package/src/api-handlers/account/change-password.ts +5 -8
  82. package/src/api-handlers/admin/analytics.ts +4 -6
  83. package/src/api-handlers/admin/audit.ts +5 -7
  84. package/src/api-handlers/admin/index.ts +1 -2
  85. package/src/api-handlers/admin/redis-sessions.ts +6 -8
  86. package/src/api-handlers/admin/sessions.ts +5 -7
  87. package/src/api-handlers/admin/site-logs.ts +8 -10
  88. package/src/api-handlers/admin/stats.ts +4 -6
  89. package/src/api-handlers/admin/users.ts +5 -7
  90. package/src/api-handlers/admin/vibe-data.ts +10 -12
  91. package/src/api-handlers/auth/refresh.ts +5 -7
  92. package/src/api-handlers/auth/signout.ts +5 -6
  93. package/src/api-handlers/auth/status.ts +4 -7
  94. package/src/api-handlers/auth/update-session.ts +123 -125
  95. package/src/api-handlers/auth/verify-code.ts +9 -13
  96. package/src/api-handlers/session/viability.ts +10 -47
  97. package/src/api-handlers/test/force-expire.ts +4 -11
  98. package/src/auth/auth-decision.ts +1 -1
  99. package/src/auth/better-auth.ts +138 -141
  100. package/src/auth/route-config.ts +219 -219
  101. package/src/auth/utils/token-utils.ts +0 -1
  102. package/src/client/AuthContext.tsx +6 -2
  103. package/src/client/better-auth-client.ts +54 -7
  104. package/src/client/fetch-with-auth.ts +47 -47
  105. package/src/components/SessionSync.tsx +6 -5
  106. package/src/components/account/MobileNavDrawer.tsx +3 -3
  107. package/src/components/account/UserAvatarMenu.tsx +6 -3
  108. package/src/components/admin/VibeAdminLayout.tsx +4 -2
  109. package/src/config/logger.ts +1 -1
  110. package/src/hooks/useAuth.ts +117 -115
  111. package/src/hooks/useAuthSettings.ts +2 -2
  112. package/src/hooks/useAvailableProviders.ts +9 -5
  113. package/src/hooks/useSessionExpiration.ts +101 -102
  114. package/src/hooks/useViabilitySession.ts +336 -335
  115. package/src/index.ts +60 -63
  116. package/src/lib/api-handler.ts +0 -1
  117. package/src/lib/app-slug.ts +6 -6
  118. package/src/lib/standardized-client-api.ts +901 -895
  119. package/src/lib/startup-init.ts +243 -247
  120. package/src/lib/test-aware-get-token.ts +22 -12
  121. package/src/lib/token-lifecycle.ts +12 -53
  122. package/src/pages/admin-login/page.tsx +9 -17
  123. package/src/pages/client-admin/ClientSiteAdminPage.tsx +4 -2
  124. package/src/pages/login/page.tsx +21 -28
  125. package/src/pages/showcase/ShowcasePage.tsx +4 -2
  126. package/src/pages/test-env/EmergencyLogoutPage.tsx +7 -6
  127. package/src/pages/test-env/JwtInspectPage.tsx +5 -3
  128. package/src/pages/test-env/RefreshTokenPage.tsx +157 -155
  129. package/src/pages/test-env/TestEnvPage.tsx +4 -2
  130. package/src/pages/verify-code/page.tsx +10 -6
  131. package/src/routes/auth/logout.ts +7 -25
  132. package/src/routes/auth/nextauth.ts +45 -71
  133. package/src/routes/auth/session.ts +25 -50
  134. package/src/routes/auth/viability.ts +7 -19
  135. package/src/server/auth.ts +60 -0
  136. package/src/stores/authStore.ts +1899 -1904
  137. package/src/utils/logout.ts +30 -30
  138. package/src/auth/auth-options.ts +0 -237
  139. package/src/auth/callbacks/index.ts +0 -7
  140. package/src/auth/callbacks/jwt.ts +0 -382
  141. package/src/auth/callbacks/session.ts +0 -243
  142. package/src/auth/callbacks/signin.ts +0 -56
  143. package/src/auth/events/index.ts +0 -5
  144. package/src/auth/events/signout.ts +0 -33
  145. package/src/auth/providers/credentials.ts +0 -256
  146. package/src/auth/providers/index.ts +0 -6
  147. package/src/auth/providers/oauth.ts +0 -114
  148. package/src/lib/nextauth-secret.ts +0 -121
  149. package/src/types/next-auth.d.ts +0 -15
@@ -1,335 +1,336 @@
1
- /**
2
- * useViabilitySession - Redis-backed session state hook
3
- *
4
- * This hook provides the REAL session state by consulting Redis via /api/session/viability
5
- * instead of relying on the potentially stale NextAuth JWT cookie.
6
- *
7
- * Redis is the single source of truth. This hook:
8
- * 1. Polls /api/session/viability to get actual session state from Redis
9
- * 2. Returns consistent auth state across all components
10
- * 3. Triggers callback when session state changes unexpectedly
11
- *
12
- * Usage:
13
- * ```tsx
14
- * const { isAuthenticated, isLoading } = useViabilitySession();
15
- * ```
16
- */
17
-
18
- 'use client';
19
-
20
- import { useState, useEffect, useRef } from 'react';
21
- import { useSession } from 'next-auth/react';
22
-
23
- export interface ViabilityState {
24
- /** Whether the user is authenticated according to Redis */
25
- isAuthenticated: boolean;
26
- /** Whether the viability check is in progress */
27
- isLoading: boolean;
28
- /** Whether 2FA is required for this client */
29
- requires2FA: boolean;
30
- /** Whether 2FA has been completed for this session */
31
- twoFactorComplete: boolean;
32
- /** Whether the access token has expired (refresh may be needed) */
33
- accessTokenExpired: boolean;
34
- /** Whether a refresh token is available */
35
- hasRefreshToken: boolean;
36
- /** Error message if viability check failed */
37
- error: string | null;
38
- /** Timestamp of last successful viability check */
39
- lastChecked: number | null;
40
- /** Force a viability check now */
41
- refresh: () => void;
42
- }
43
-
44
- interface ViabilityResponse {
45
- authenticated: boolean;
46
- sessionToken?: string;
47
- requires2FA?: boolean;
48
- twoFactorComplete?: boolean;
49
- accessTokenExpired?: boolean;
50
- hasRefreshToken?: boolean;
51
- }
52
-
53
- export interface UseViabilitySessionOptions {
54
- /** Polling interval in milliseconds (default: 30000 = 30 seconds) */
55
- pollInterval?: number;
56
- /** Whether to poll automatically (default: true) */
57
- enablePolling?: boolean;
58
- /** Callback when session becomes invalid */
59
- onSessionInvalid?: () => void;
60
- }
61
-
62
- // Use window to persist state across HMR in development
63
- declare global {
64
- interface Window {
65
- __viabilitySessionState?: {
66
- isAuthenticated: boolean;
67
- isLoading: boolean;
68
- requires2FA: boolean;
69
- twoFactorComplete: boolean;
70
- accessTokenExpired: boolean;
71
- hasRefreshToken: boolean;
72
- error: string | null;
73
- lastChecked: number | null;
74
- checkInProgress: boolean;
75
- prevAuth: boolean | null;
76
- intervalId: ReturnType<typeof setInterval> | null;
77
- listeners: Set<() => void>;
78
- onSessionInvalidCallbacks: Set<() => void>;
79
- };
80
- }
81
- }
82
-
83
- function getGlobalState() {
84
- if (typeof window === 'undefined') {
85
- // SSR - return default state
86
- return {
87
- isAuthenticated: false,
88
- isLoading: true,
89
- requires2FA: false,
90
- twoFactorComplete: false,
91
- accessTokenExpired: false,
92
- hasRefreshToken: false,
93
- error: null,
94
- lastChecked: null,
95
- checkInProgress: false,
96
- prevAuth: null,
97
- intervalId: null,
98
- listeners: new Set<() => void>(),
99
- onSessionInvalidCallbacks: new Set<() => void>()
100
- };
101
- }
102
-
103
- // Initialize global state on window if not present
104
- if (!window.__viabilitySessionState) {
105
- window.__viabilitySessionState = {
106
- isAuthenticated: false,
107
- isLoading: true,
108
- requires2FA: false,
109
- twoFactorComplete: false,
110
- accessTokenExpired: false,
111
- hasRefreshToken: false,
112
- error: null,
113
- lastChecked: null,
114
- checkInProgress: false,
115
- prevAuth: null,
116
- intervalId: null,
117
- listeners: new Set<() => void>(),
118
- onSessionInvalidCallbacks: new Set<() => void>()
119
- };
120
- }
121
-
122
- return window.__viabilitySessionState;
123
- }
124
-
125
- async function doViabilityCheck(): Promise<void> {
126
- const state = getGlobalState();
127
-
128
- // Prevent concurrent checks
129
- if (state.checkInProgress) return;
130
- state.checkInProgress = true;
131
-
132
- try {
133
- const response = await fetch('/api/session/viability', {
134
- method: 'GET',
135
- headers: {
136
- 'Accept': 'application/json',
137
- 'Cache-Control': 'no-store'
138
- },
139
- credentials: 'include'
140
- });
141
-
142
- if (!response.ok) {
143
- state.isLoading = false;
144
- state.error = `Viability check failed: ${response.status}`;
145
- state.lastChecked = Date.now();
146
- notifyListeners();
147
- return;
148
- }
149
-
150
- const data: ViabilityResponse = await response.json();
151
-
152
- // Detect auth state change
153
- if (state.prevAuth !== null && state.prevAuth !== data.authenticated) {
154
- console.log('[useViabilitySession] Auth state changed:', {
155
- was: state.prevAuth,
156
- now: data.authenticated
157
- });
158
-
159
- if (!data.authenticated) {
160
- // Notify all callbacks
161
- state.onSessionInvalidCallbacks.forEach(cb => {
162
- try { cb(); } catch (e) { console.error('[useViabilitySession] onSessionInvalid error:', e); }
163
- });
164
- }
165
- }
166
-
167
- state.prevAuth = data.authenticated;
168
- state.isAuthenticated = data.authenticated;
169
- state.isLoading = false;
170
- state.requires2FA = data.requires2FA ?? false;
171
- state.twoFactorComplete = data.twoFactorComplete ?? false;
172
- state.accessTokenExpired = data.accessTokenExpired ?? false;
173
- state.hasRefreshToken = data.hasRefreshToken ?? false;
174
- state.error = null;
175
- state.lastChecked = Date.now();
176
-
177
- notifyListeners();
178
-
179
- } catch (error) {
180
- console.error('[useViabilitySession] Error checking viability:', error);
181
- const state = getGlobalState();
182
- state.isLoading = false;
183
- state.error = error instanceof Error ? error.message : 'Unknown error';
184
- state.lastChecked = Date.now();
185
- notifyListeners();
186
- } finally {
187
- getGlobalState().checkInProgress = false;
188
- }
189
- }
190
-
191
- function notifyListeners() {
192
- const state = getGlobalState();
193
- state.listeners.forEach(listener => {
194
- try { listener(); } catch (e) { /* ignore */ }
195
- });
196
- }
197
-
198
- function startPolling(interval: number) {
199
- const state = getGlobalState();
200
- if (state.intervalId !== null) return; // Already polling
201
-
202
- state.intervalId = setInterval(() => {
203
- doViabilityCheck();
204
- }, interval);
205
- }
206
-
207
- function stopPolling() {
208
- const state = getGlobalState();
209
- if (state.intervalId !== null) {
210
- clearInterval(state.intervalId);
211
- state.intervalId = null;
212
- }
213
- }
214
-
215
- /**
216
- * Hook that provides Redis-backed session state
217
- */
218
- export function useViabilitySession(options: UseViabilitySessionOptions = {}): ViabilityState {
219
- const {
220
- pollInterval = 30000,
221
- enablePolling = true,
222
- onSessionInvalid
223
- } = options;
224
-
225
- const { status: nextAuthStatus } = useSession();
226
- const [, forceUpdate] = useState(0);
227
- const mountedRef = useRef(true);
228
- const initializedRef = useRef(false);
229
-
230
- // Register this component's onSessionInvalid callback
231
- useEffect(() => {
232
- if (onSessionInvalid) {
233
- const state = getGlobalState();
234
- state.onSessionInvalidCallbacks.add(onSessionInvalid);
235
- return () => {
236
- state.onSessionInvalidCallbacks.delete(onSessionInvalid);
237
- };
238
- }
239
- }, [onSessionInvalid]);
240
-
241
- // Subscribe to global state changes
242
- useEffect(() => {
243
- mountedRef.current = true;
244
- const listener = () => {
245
- if (mountedRef.current) {
246
- forceUpdate(n => n + 1);
247
- }
248
- };
249
- const state = getGlobalState();
250
- state.listeners.add(listener);
251
-
252
- return () => {
253
- mountedRef.current = false;
254
- state.listeners.delete(listener);
255
- };
256
- }, []);
257
-
258
- // Initial check when NextAuth status is determined - only once!
259
- useEffect(() => {
260
- if (nextAuthStatus === 'loading') {
261
- return;
262
- }
263
-
264
- const state = getGlobalState();
265
-
266
- // Only do initial check once globally
267
- if (!initializedRef.current && state.lastChecked === null) {
268
- initializedRef.current = true;
269
- doViabilityCheck();
270
- }
271
- }, [nextAuthStatus]);
272
-
273
- // Manage polling - only one interval for all hook instances
274
- useEffect(() => {
275
- if (!enablePolling || nextAuthStatus === 'loading') {
276
- return;
277
- }
278
-
279
- // Start polling if not already started
280
- startPolling(pollInterval);
281
-
282
- // Cleanup: only stop if this is the last listener
283
- return () => {
284
- const state = getGlobalState();
285
- // Small delay to allow other components to register
286
- setTimeout(() => {
287
- if (state.listeners.size === 0) {
288
- stopPolling();
289
- }
290
- }, 100);
291
- };
292
- }, [enablePolling, pollInterval, nextAuthStatus]);
293
-
294
- // Check viability on focus (user returns to tab) - with debounce
295
- useEffect(() => {
296
- const handleFocus = () => {
297
- const state = getGlobalState();
298
- // Debounce: only check if last check was > 10 seconds ago
299
- if (state.lastChecked !== null &&
300
- Date.now() - state.lastChecked > 10000) {
301
- doViabilityCheck();
302
- }
303
- };
304
-
305
- window.addEventListener('focus', handleFocus);
306
- return () => window.removeEventListener('focus', handleFocus);
307
- }, []);
308
-
309
- // Return current state
310
- const state = getGlobalState();
311
- return {
312
- isAuthenticated: state.isAuthenticated,
313
- isLoading: state.isLoading,
314
- requires2FA: state.requires2FA,
315
- twoFactorComplete: state.twoFactorComplete,
316
- accessTokenExpired: state.accessTokenExpired,
317
- hasRefreshToken: state.hasRefreshToken,
318
- error: state.error,
319
- lastChecked: state.lastChecked,
320
- refresh: doViabilityCheck
321
- };
322
- }
323
-
324
- /**
325
- * Simplified hook that just returns authentication status
326
- * Use this in components that only need to know if user is logged in
327
- */
328
- export function useIsAuthenticated(): { isAuthenticated: boolean; isLoading: boolean } {
329
- const { isAuthenticated, isLoading } = useViabilitySession({
330
- pollInterval: 60000, // Less frequent polling for simple status
331
- enablePolling: true
332
- });
333
-
334
- return { isAuthenticated, isLoading };
335
- }
1
+ /**
2
+ * useViabilitySession - Redis-backed session state hook
3
+ *
4
+ * This hook provides the REAL session state by consulting Redis via /api/session/viability
5
+ * instead of relying on the potentially stale NextAuth JWT cookie.
6
+ *
7
+ * Redis is the single source of truth. This hook:
8
+ * 1. Polls /api/session/viability to get actual session state from Redis
9
+ * 2. Returns consistent auth state across all components
10
+ * 3. Triggers callback when session state changes unexpectedly
11
+ *
12
+ * Usage:
13
+ * ```tsx
14
+ * const { isAuthenticated, isLoading } = useViabilitySession();
15
+ * ```
16
+ */
17
+
18
+ 'use client';
19
+
20
+ import { useState, useEffect, useRef } from 'react';
21
+ import { authClient } from '../client/better-auth-client';
22
+
23
+ export interface ViabilityState {
24
+ /** Whether the user is authenticated according to Redis */
25
+ isAuthenticated: boolean;
26
+ /** Whether the viability check is in progress */
27
+ isLoading: boolean;
28
+ /** Whether 2FA is required for this client */
29
+ requires2FA: boolean;
30
+ /** Whether 2FA has been completed for this session */
31
+ twoFactorComplete: boolean;
32
+ /** Whether the access token has expired (refresh may be needed) */
33
+ accessTokenExpired: boolean;
34
+ /** Whether a refresh token is available */
35
+ hasRefreshToken: boolean;
36
+ /** Error message if viability check failed */
37
+ error: string | null;
38
+ /** Timestamp of last successful viability check */
39
+ lastChecked: number | null;
40
+ /** Force a viability check now */
41
+ refresh: () => void;
42
+ }
43
+
44
+ interface ViabilityResponse {
45
+ authenticated: boolean;
46
+ sessionToken?: string;
47
+ requires2FA?: boolean;
48
+ twoFactorComplete?: boolean;
49
+ accessTokenExpired?: boolean;
50
+ hasRefreshToken?: boolean;
51
+ }
52
+
53
+ export interface UseViabilitySessionOptions {
54
+ /** Polling interval in milliseconds (default: 30000 = 30 seconds) */
55
+ pollInterval?: number;
56
+ /** Whether to poll automatically (default: true) */
57
+ enablePolling?: boolean;
58
+ /** Callback when session becomes invalid */
59
+ onSessionInvalid?: () => void;
60
+ }
61
+
62
+ // Use window to persist state across HMR in development
63
+ declare global {
64
+ interface Window {
65
+ __viabilitySessionState?: {
66
+ isAuthenticated: boolean;
67
+ isLoading: boolean;
68
+ requires2FA: boolean;
69
+ twoFactorComplete: boolean;
70
+ accessTokenExpired: boolean;
71
+ hasRefreshToken: boolean;
72
+ error: string | null;
73
+ lastChecked: number | null;
74
+ checkInProgress: boolean;
75
+ prevAuth: boolean | null;
76
+ intervalId: ReturnType<typeof setInterval> | null;
77
+ listeners: Set<() => void>;
78
+ onSessionInvalidCallbacks: Set<() => void>;
79
+ };
80
+ }
81
+ }
82
+
83
+ function getGlobalState() {
84
+ if (typeof window === 'undefined') {
85
+ // SSR - return default state
86
+ return {
87
+ isAuthenticated: false,
88
+ isLoading: true,
89
+ requires2FA: false,
90
+ twoFactorComplete: false,
91
+ accessTokenExpired: false,
92
+ hasRefreshToken: false,
93
+ error: null,
94
+ lastChecked: null,
95
+ checkInProgress: false,
96
+ prevAuth: null,
97
+ intervalId: null,
98
+ listeners: new Set<() => void>(),
99
+ onSessionInvalidCallbacks: new Set<() => void>()
100
+ };
101
+ }
102
+
103
+ // Initialize global state on window if not present
104
+ if (!window.__viabilitySessionState) {
105
+ window.__viabilitySessionState = {
106
+ isAuthenticated: false,
107
+ isLoading: true,
108
+ requires2FA: false,
109
+ twoFactorComplete: false,
110
+ accessTokenExpired: false,
111
+ hasRefreshToken: false,
112
+ error: null,
113
+ lastChecked: null,
114
+ checkInProgress: false,
115
+ prevAuth: null,
116
+ intervalId: null,
117
+ listeners: new Set<() => void>(),
118
+ onSessionInvalidCallbacks: new Set<() => void>()
119
+ };
120
+ }
121
+
122
+ return window.__viabilitySessionState;
123
+ }
124
+
125
+ async function doViabilityCheck(): Promise<void> {
126
+ const state = getGlobalState();
127
+
128
+ // Prevent concurrent checks
129
+ if (state.checkInProgress) return;
130
+ state.checkInProgress = true;
131
+
132
+ try {
133
+ const response = await fetch('/api/session/viability', {
134
+ method: 'GET',
135
+ headers: {
136
+ 'Accept': 'application/json',
137
+ 'Cache-Control': 'no-store'
138
+ },
139
+ credentials: 'include'
140
+ });
141
+
142
+ if (!response.ok) {
143
+ state.isLoading = false;
144
+ state.error = `Viability check failed: ${response.status}`;
145
+ state.lastChecked = Date.now();
146
+ notifyListeners();
147
+ return;
148
+ }
149
+
150
+ const data: ViabilityResponse = await response.json();
151
+
152
+ // Detect auth state change
153
+ if (state.prevAuth !== null && state.prevAuth !== data.authenticated) {
154
+ console.log('[useViabilitySession] Auth state changed:', {
155
+ was: state.prevAuth,
156
+ now: data.authenticated
157
+ });
158
+
159
+ if (!data.authenticated) {
160
+ // Notify all callbacks
161
+ state.onSessionInvalidCallbacks.forEach(cb => {
162
+ try { cb(); } catch (e) { console.error('[useViabilitySession] onSessionInvalid error:', e); }
163
+ });
164
+ }
165
+ }
166
+
167
+ state.prevAuth = data.authenticated;
168
+ state.isAuthenticated = data.authenticated;
169
+ state.isLoading = false;
170
+ state.requires2FA = data.requires2FA ?? false;
171
+ state.twoFactorComplete = data.twoFactorComplete ?? false;
172
+ state.accessTokenExpired = data.accessTokenExpired ?? false;
173
+ state.hasRefreshToken = data.hasRefreshToken ?? false;
174
+ state.error = null;
175
+ state.lastChecked = Date.now();
176
+
177
+ notifyListeners();
178
+
179
+ } catch (error) {
180
+ console.error('[useViabilitySession] Error checking viability:', error);
181
+ const state = getGlobalState();
182
+ state.isLoading = false;
183
+ state.error = error instanceof Error ? error.message : 'Unknown error';
184
+ state.lastChecked = Date.now();
185
+ notifyListeners();
186
+ } finally {
187
+ getGlobalState().checkInProgress = false;
188
+ }
189
+ }
190
+
191
+ function notifyListeners() {
192
+ const state = getGlobalState();
193
+ state.listeners.forEach(listener => {
194
+ try { listener(); } catch (e) { /* ignore */ }
195
+ });
196
+ }
197
+
198
+ function startPolling(interval: number) {
199
+ const state = getGlobalState();
200
+ if (state.intervalId !== null) return; // Already polling
201
+
202
+ state.intervalId = setInterval(() => {
203
+ doViabilityCheck();
204
+ }, interval);
205
+ }
206
+
207
+ function stopPolling() {
208
+ const state = getGlobalState();
209
+ if (state.intervalId !== null) {
210
+ clearInterval(state.intervalId);
211
+ state.intervalId = null;
212
+ }
213
+ }
214
+
215
+ /**
216
+ * Hook that provides Redis-backed session state
217
+ */
218
+ export function useViabilitySession(options: UseViabilitySessionOptions = {}): ViabilityState {
219
+ const {
220
+ pollInterval = 30000,
221
+ enablePolling = true,
222
+ onSessionInvalid
223
+ } = options;
224
+
225
+ const { data: _sessionData, isPending } = authClient.useSession();
226
+ const nextAuthStatus = isPending ? 'loading' : _sessionData ? 'authenticated' : 'unauthenticated';
227
+ const [, forceUpdate] = useState(0);
228
+ const mountedRef = useRef(true);
229
+ const initializedRef = useRef(false);
230
+
231
+ // Register this component's onSessionInvalid callback
232
+ useEffect(() => {
233
+ if (onSessionInvalid) {
234
+ const state = getGlobalState();
235
+ state.onSessionInvalidCallbacks.add(onSessionInvalid);
236
+ return () => {
237
+ state.onSessionInvalidCallbacks.delete(onSessionInvalid);
238
+ };
239
+ }
240
+ }, [onSessionInvalid]);
241
+
242
+ // Subscribe to global state changes
243
+ useEffect(() => {
244
+ mountedRef.current = true;
245
+ const listener = () => {
246
+ if (mountedRef.current) {
247
+ forceUpdate(n => n + 1);
248
+ }
249
+ };
250
+ const state = getGlobalState();
251
+ state.listeners.add(listener);
252
+
253
+ return () => {
254
+ mountedRef.current = false;
255
+ state.listeners.delete(listener);
256
+ };
257
+ }, []);
258
+
259
+ // Initial check when NextAuth status is determined - only once!
260
+ useEffect(() => {
261
+ if (nextAuthStatus === 'loading') {
262
+ return;
263
+ }
264
+
265
+ const state = getGlobalState();
266
+
267
+ // Only do initial check once globally
268
+ if (!initializedRef.current && state.lastChecked === null) {
269
+ initializedRef.current = true;
270
+ doViabilityCheck();
271
+ }
272
+ }, [nextAuthStatus]);
273
+
274
+ // Manage polling - only one interval for all hook instances
275
+ useEffect(() => {
276
+ if (!enablePolling || nextAuthStatus === 'loading') {
277
+ return;
278
+ }
279
+
280
+ // Start polling if not already started
281
+ startPolling(pollInterval);
282
+
283
+ // Cleanup: only stop if this is the last listener
284
+ return () => {
285
+ const state = getGlobalState();
286
+ // Small delay to allow other components to register
287
+ setTimeout(() => {
288
+ if (state.listeners.size === 0) {
289
+ stopPolling();
290
+ }
291
+ }, 100);
292
+ };
293
+ }, [enablePolling, pollInterval, nextAuthStatus]);
294
+
295
+ // Check viability on focus (user returns to tab) - with debounce
296
+ useEffect(() => {
297
+ const handleFocus = () => {
298
+ const state = getGlobalState();
299
+ // Debounce: only check if last check was > 10 seconds ago
300
+ if (state.lastChecked !== null &&
301
+ Date.now() - state.lastChecked > 10000) {
302
+ doViabilityCheck();
303
+ }
304
+ };
305
+
306
+ window.addEventListener('focus', handleFocus);
307
+ return () => window.removeEventListener('focus', handleFocus);
308
+ }, []);
309
+
310
+ // Return current state
311
+ const state = getGlobalState();
312
+ return {
313
+ isAuthenticated: state.isAuthenticated,
314
+ isLoading: state.isLoading,
315
+ requires2FA: state.requires2FA,
316
+ twoFactorComplete: state.twoFactorComplete,
317
+ accessTokenExpired: state.accessTokenExpired,
318
+ hasRefreshToken: state.hasRefreshToken,
319
+ error: state.error,
320
+ lastChecked: state.lastChecked,
321
+ refresh: doViabilityCheck
322
+ };
323
+ }
324
+
325
+ /**
326
+ * Simplified hook that just returns authentication status
327
+ * Use this in components that only need to know if user is logged in
328
+ */
329
+ export function useIsAuthenticated(): { isAuthenticated: boolean; isLoading: boolean } {
330
+ const { isAuthenticated, isLoading } = useViabilitySession({
331
+ pollInterval: 60000, // Less frequent polling for simple status
332
+ enablePolling: true
333
+ });
334
+
335
+ return { isAuthenticated, isLoading };
336
+ }