11import { inIframe } from "../utils/inIframe" ;
22import { toast } from "react-toastify" ;
33import retryManager from "../utils/retryFetch" ;
4+ import { useState , useEffect } from "react" ;
45
56// secret: userDb.secret, username: userDb.username, email: userDb.email, staff: userDb.staff, canMakeClues: userDb.canMakeClues, supporter: userDb.supporter
67let session = false ;
78// null = not logged in
89// false = session loading/fetching
910
11+ // Listeners for session changes
12+ const sessionListeners = new Set ( ) ;
13+ function notifySessionChange ( ) {
14+ sessionListeners . forEach ( listener => listener ( session ) ) ;
15+ }
16+
1017export function signOut ( ) {
1118 window . localStorage . removeItem ( "wg_secret" ) ;
1219 session = null ;
20+ notifySessionChange ( ) ;
1321 if ( window . dontReconnect ) {
1422 return ;
1523 }
@@ -58,6 +66,18 @@ export function signIn() {
5866}
5967
6068export function useSession ( ) {
69+ // sessionState is only used to trigger re-renders when session changes
70+ const [ , setSessionState ] = useState ( session ) ;
71+
72+ // Subscribe to session changes
73+ useEffect ( ( ) => {
74+ const listener = ( newSession ) => {
75+ setSessionState ( newSession ) ;
76+ } ;
77+ sessionListeners . add ( listener ) ;
78+ return ( ) => sessionListeners . delete ( listener ) ;
79+ } , [ ] ) ;
80+
6181 if ( typeof window === "undefined" ) {
6282 return {
6383 data : false
@@ -88,6 +108,7 @@ export function useSession() {
88108
89109 window . fetchingSession = true ;
90110
111+ const authStartTime = performance . now ( ) ;
91112 console . log ( `[Auth] Starting authentication with retry mechanism (5s timeout, unlimited retries)` ) ;
92113
93114 retryManager . fetchWithRetry (
@@ -110,26 +131,31 @@ export function useSession() {
110131 . then ( ( res ) => res . json ( ) )
111132 . then ( ( data ) => {
112133 window . fetchingSession = false ;
113- console . log ( `[Auth] Authentication successful` ) ;
134+ const authDuration = ( performance . now ( ) - authStartTime ) . toFixed ( 0 ) ;
135+ console . log ( `[Auth] Authentication successful (took ${ authDuration } ms)` ) ;
114136
115137 if ( data . error ) {
116138 console . error ( `[Auth] Server error:` , data . error ) ;
117139 session = null ;
140+ notifySessionChange ( ) ;
118141 return ;
119142 }
120143
121144 if ( data . secret ) {
122145 window . localStorage . setItem ( "wg_secret" , data . secret ) ;
123146 session = { token : data } ;
124147 console . log ( `[Auth] Session established for user:` , data . username ) ;
148+ notifySessionChange ( ) ;
125149 } else {
126150 console . log ( `[Auth] No session data received, user not logged in` ) ;
127151 session = null ;
152+ notifySessionChange ( ) ;
128153 }
129154 } )
130155 . catch ( ( e ) => {
131156 window . fetchingSession = false ;
132- console . error ( `[Auth] Authentication failed:` , e . message ) ;
157+ const authDuration = ( performance . now ( ) - authStartTime ) . toFixed ( 0 ) ;
158+ console . error ( `[Auth] Authentication failed (took ${ authDuration } ms):` , e . message ) ;
133159
134160 // Clear potentially corrupted session data
135161 try {
@@ -139,9 +165,11 @@ export function useSession() {
139165 }
140166
141167 session = null ;
168+ notifySessionChange ( ) ;
142169 } ) ;
143170 } else {
144171 session = null ;
172+ notifySessionChange ( ) ;
145173 }
146174 }
147175
0 commit comments