Skip to content

Commit 106a409

Browse files
Manual OAuth code interceptor to fix mobile redirect issues
1 parent f00af6e commit 106a409

1 file changed

Lines changed: 31 additions & 36 deletions

File tree

web/src/store/useStore.ts

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,22 @@ export const useStore = create<AppState>()(
5757
setLoading: (loading) => set({ isLoading: loading }),
5858
setError: (error) => set({ error: error }),
5959

60-
initAuth: () => {
60+
initAuth: async () => {
6161
set({ isAuthLoading: true });
6262

63-
// Set up listener for all auth events
63+
// 1. Manually check for code in URL (especially for mobile/HashRouter)
64+
const params = new URLSearchParams(window.location.search);
65+
const code = params.get('code');
66+
67+
if (code) {
68+
console.log('Detected OAuth code, exchanging...');
69+
await supabase.auth.exchangeCodeForSession(code);
70+
// Clean up URL to prevent multiple exchanges
71+
const newUrl = window.location.origin + window.location.pathname + window.location.hash;
72+
window.history.replaceState({}, document.title, newUrl);
73+
}
74+
75+
// 2. Set up listener
6476
supabase.auth.onAuthStateChange(async (event, session) => {
6577
console.log('Auth event:', event, !!session);
6678

@@ -84,47 +96,30 @@ export const useStore = create<AppState>()(
8496
}
8597
} else {
8698
set({ user: null });
87-
88-
// Only end loading if there are no auth params in URL
89-
const hasParams = window.location.href.includes('code=') || window.location.href.includes('access_token=');
90-
if (!hasParams) {
99+
// Only stop loading if we're not currently exchanging a code
100+
if (!code) {
91101
set({ isAuthLoading: false });
92-
93-
// If signed out and no ongoing OAuth, then sign in anonymously
94-
if (event === 'SIGNED_OUT') {
95-
supabase.auth.signInAnonymously();
96-
}
97102
}
98103
}
99104
});
100105

101-
// Initial check with a safety timeout for OAuth
102-
supabase.auth.getSession().then(({ data: { session } }) => {
103-
if (session) {
104-
set({ user: session.user, isAuthLoading: false });
105-
} else {
106-
const hasParams = window.location.href.includes('code=') || window.location.href.includes('access_token=');
107-
108-
if (!hasParams) {
109-
// Longer delay for mobile to process redirect
110-
setTimeout(() => {
111-
const currentSession = get().user;
112-
if (!currentSession) {
113-
supabase.auth.signInAnonymously().then(() => {
114-
set({ isAuthLoading: false });
115-
});
116-
} else {
117-
set({ isAuthLoading: false });
118-
}
119-
}, 3000);
106+
// 3. Initial session check
107+
const { data: { session } } = await supabase.auth.getSession();
108+
if (session) {
109+
set({ user: session.user, isAuthLoading: false });
110+
} else if (!code) {
111+
// If no session and no code, we're definitely a guest
112+
// But let's wait a bit before signing in anonymously
113+
setTimeout(() => {
114+
if (!get().user) {
115+
supabase.auth.signInAnonymously().then(() => {
116+
set({ isAuthLoading: false });
117+
});
120118
} else {
121-
// Wait for onAuthStateChange to fire from URL detection
122-
setTimeout(() => {
123-
set({ isAuthLoading: false });
124-
}, 5000);
119+
set({ isAuthLoading: false });
125120
}
126-
}
127-
});
121+
}, 2000);
122+
}
128123
},
129124

130125
signInWithGoogle: async () => {

0 commit comments

Comments
 (0)