@@ -58,9 +58,21 @@ export const useStore = create<AppState>()(
5858 setError : ( error ) => set ( { error : error } ) ,
5959
6060 initAuth : async ( ) => {
61- set ( { isAuthLoading : true } ) ;
61+ // Capture code immediately from URL before Router mangles it
62+ const params = new URLSearchParams ( window . location . search ) ;
63+ const hasCode = params . has ( 'code' ) ;
64+
65+ if ( hasCode ) {
66+ set ( { isAuthLoading : true } ) ;
67+ } else {
68+ // Check session to decide if we need to show loading
69+ const { data : { session } } = await supabase . auth . getSession ( ) ;
70+ if ( ! session ) {
71+ set ( { isAuthLoading : false } ) ;
72+ }
73+ }
6274
63- // 1. Set up listener
75+ // Set up listener
6476 supabase . auth . onAuthStateChange ( async ( event , session ) => {
6577 console . log ( 'Auth event:' , event , ! ! session ) ;
6678 localStorage . setItem ( 'sssk_last_auth_event' , `${ event } at ${ new Date ( ) . toLocaleTimeString ( ) } ` ) ;
@@ -89,32 +101,19 @@ export const useStore = create<AppState>()(
89101 if ( window . location . search . includes ( 'code=' ) ) {
90102 window . location . hash = '#/cabinet' ;
91103 }
92- } else {
93- set ( { user : null } ) ;
94- // If no ongoing OAuth, stop loading
95- if ( ! window . location . search . includes ( 'code=' ) ) {
96- set ( { isAuthLoading : false } ) ;
97- }
104+ } else if ( event === 'INITIAL_SESSION' && ! hasCode ) {
105+ set ( { user : null , isAuthLoading : false } ) ;
106+ } else if ( event === 'SIGNED_OUT' ) {
107+ set ( { user : null , isAuthLoading : false } ) ;
98108 }
99109 } ) ;
100110
101- // 2. Initial check
102- const { data : { session } } = await supabase . auth . getSession ( ) ;
103- if ( session ) {
104- set ( { user : session . user , isAuthLoading : false } ) ;
105- } else {
106- // If no session and no code, we are done
107- if ( ! window . location . search . includes ( 'code=' ) ) {
111+ // Safety timeout for the "Instant Skip"
112+ setTimeout ( ( ) => {
113+ if ( get ( ) . isAuthLoading ) {
108114 set ( { isAuthLoading : false } ) ;
109- } else {
110- // Safety timeout: if after 10s we still don't have a session but have a code, stop loading
111- setTimeout ( ( ) => {
112- if ( get ( ) . isAuthLoading ) {
113- set ( { isAuthLoading : false } ) ;
114- }
115- } , 10000 ) ;
116115 }
117- }
116+ } , 8000 ) ;
118117 } ,
119118
120119 signInWithGoogle : async ( ) => {
0 commit comments