Skip to content

Commit d58831e

Browse files
Fix instant skip by capturing auth code before routing and resetting supabase storage to defaults
1 parent d04d5e7 commit d58831e

2 files changed

Lines changed: 23 additions & 26 deletions

File tree

web/src/services/supabaseClient.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
1111
auth: {
1212
persistSession: true,
1313
autoRefreshToken: true,
14-
detectSessionInUrl: true,
15-
storageKey: 'sssk-auth-token',
16-
storage: window.localStorage
14+
detectSessionInUrl: true
1715
}
1816
});

web/src/store/useStore.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)