@@ -10,32 +10,49 @@ export const useSessionCookie = () => {
1010 /**
1111 * Creates or refreshes the session cookie.
1212 * Called after login and on token refresh.
13+ * Implements retry logic with token refresh for handling timing issues.
1314 */
1415 const createSession = async ( ) : Promise < void > => {
1516 if ( ! isCloud ) return
1617
1718 const authStore = useFirebaseAuthStore ( )
18- const authHeader = await authStore . getAuthHeader ( )
1919
20- if ( ! authHeader ) {
21- throw new Error ( 'No auth header available for session creation' )
22- }
20+ // Simple retry with forceRefresh for token timing issues
21+ for ( let attempt = 0 ; attempt < 3 ; attempt ++ ) {
22+ // First attempt uses cached token, retries force refresh
23+ const authHeader = await authStore . getAuthHeader ( attempt > 0 )
2324
24- const response = await fetch ( api . apiURL ( '/auth/session' ) , {
25- method : 'POST' ,
26- credentials : 'include' ,
27- headers : {
28- ...authHeader ,
29- 'Content-Type' : 'application/json'
25+ if ( authHeader ) {
26+ // Successfully got auth header, proceed with session creation
27+ const response = await fetch ( api . apiURL ( '/auth/session' ) , {
28+ method : 'POST' ,
29+ credentials : 'include' ,
30+ headers : {
31+ ...authHeader ,
32+ 'Content-Type' : 'application/json'
33+ }
34+ } )
35+
36+ if ( ! response . ok ) {
37+ const errorData = await response . json ( ) . catch ( ( ) => ( { } ) )
38+ throw new Error (
39+ `Failed to create session: ${ errorData . message || response . statusText } `
40+ )
41+ }
42+
43+ return // Success
3044 }
31- } )
3245
33- if ( ! response . ok ) {
34- const errorData = await response . json ( ) . catch ( ( ) => ( { } ) )
35- throw new Error (
36- `Failed to create session: ${ errorData . message || response . statusText } `
37- )
46+ // Exponential backoff before retry (except for last attempt)
47+ if ( attempt < 2 ) {
48+ await new Promise ( ( r ) => setTimeout ( r , Math . pow ( 2 , attempt ) * 500 ) )
49+ }
3850 }
51+
52+ // Failed to get auth header after 3 attempts
53+ throw new Error (
54+ 'No auth header available for session creation after retries'
55+ )
3956 }
4057
4158 /**
0 commit comments