Repository navigation
Expand file tree
/
Copy pathAuthProvider.tsx
More file actions
546 lines (499 loc) · 23 KB
/
Copy pathAuthProvider.tsx
File metadata and controls
546 lines (499 loc) · 23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/* eslint-disable react-refresh/only-export-components */
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import type { Session } from '@supabase/supabase-js';
import { AUTH_STORAGE_KEY, setActiveAccessToken, supabase } from './client';
import { allowSessionsAfterSignIn, rejectDeadSession, restoredSessionsBlocked } from './session-expiry';
import { clearSignOutIntent, markSignOutIntent } from './sign-out-intent';
import { AuthContext, useAuth, type AuthContextValue } from './auth-context';
import { useSessionStore } from '@/lib/stores/session-store';
import { useSettingsStore } from '@/lib/stores/settings-store';
import { useSparringStore } from '@/lib/stores/sparring-store';
import { useDebateStore } from '@/lib/stores/debate-store';
import { useDebateContextStore } from '@/lib/stores/debate-context-store';
import { openExternalUrl } from '@/lib/native';
import {
AUTH_CALLBACK_PATH,
authCallbackErrorFor,
authCallbackUrl,
readAuthCallbackParams,
withoutCallbackParams,
type AuthCallbackError,
} from '@/lib/auth-callback';
import { setJamUserId } from '@/lib/jam-metadata';
import { clearCrossfireDraftOnAuthChange } from '@/lib/crossfire/storage';
import { abortToolRuns } from '@/components/features/tools/tool-run';
const AUTH_OPERATION_TIMEOUT_MS = 4000;
const OAUTH_CODE_EXCHANGE_TIMEOUT_MS = 15_000;
const DEMO_FINGERPRINT_STORAGE_KEY = 'debate-demo-browser-fingerprint';
type DemoCodeHandoff = {
email: string;
tokenHash: string;
};
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function parseDemoCodeHandoff(value: unknown): DemoCodeHandoff {
if (!isRecord(value) || typeof value.email !== 'string' || value.type !== 'magiclink') {
throw new Error('Demo sign-in returned an invalid session handoff.');
}
const tokenHash = typeof value.tokenHash === 'string'
? value.tokenHash
: typeof value.token_hash === 'string'
? value.token_hash
: '';
const email = value.email.trim();
if (!email || !tokenHash) throw new Error('Demo sign-in returned an invalid session handoff.');
return {
email,
tokenHash,
};
}
async function getFunctionError(error: unknown) {
if (isRecord(error) && error.context instanceof Response) {
try {
const body: unknown = await error.context.json();
if (isRecord(body) && typeof body.error === 'string' && body.error) return new Error(body.error);
} catch {
// Fall back to the SDK error below when the response is not JSON.
}
}
return error instanceof Error ? error : new Error('Demo sign-in failed.');
}
function getDemoBrowserFingerprint() {
try {
const existing = sessionStorage.getItem(DEMO_FINGERPRINT_STORAGE_KEY);
if (existing) return existing;
const fingerprint = typeof globalThis.crypto?.randomUUID === 'function'
? globalThis.crypto.randomUUID()
: `${Date.now()}-${Math.random().toString(36).slice(2)}`;
sessionStorage.setItem(DEMO_FINGERPRINT_STORAGE_KEY, fingerprint);
return fingerprint;
} catch {
return 'unavailable';
}
}
class AuthTimeoutError extends Error {
constructor(message: string) {
super(message);
this.name = 'AuthTimeoutError';
}
}
/** What a code exchange that outlived its timeout finally answered. */
type LateAnswer = { session: Session | null; lateError: unknown };
function withAuthTimeout<T>(operation: Promise<T>, message: string, timeoutMs = AUTH_OPERATION_TIMEOUT_MS): Promise<T> {
return Promise.race([
operation,
new Promise<never>((_, reject) => setTimeout(() => reject(new AuthTimeoutError(message)), timeoutMs)),
]);
}
function isDeadUserLookup(error: unknown): boolean {
if (!error || typeof error !== 'object' || !('status' in error)) return false;
const status = (error as { status?: unknown }).status;
return status === 401 || status === 403;
}
function clearGoogleTokens() {
sessionStorage.removeItem('google_provider_token');
localStorage.removeItem('google_provider_token');
// Remove refresh tokens written by older clients as well; this client
// never persists them because they are long-lived credentials.
localStorage.removeItem('google_provider_refresh_token');
}
export function AuthProvider({ children }: { children: React.ReactNode }) {
const [session, setSession] = useState<Session | null>(null);
const [loading, setLoading] = useState(true);
const [callbackError, setCallbackError] = useState<AuthCallbackError | null>(null);
const hydrateSessionStore = useSessionStore((state) => state.hydrateFromServer);
const prepareSessionStore = useSessionStore((state) => state.prepareForUser);
const hydrateSettingsStore = useSettingsStore((state) => state.hydrateFromServer);
const prepareSettingsStore = useSettingsStore((state) => state.prepareForUser);
const hydrateSparringStore = useSparringStore((state) => state.hydrateSavedSessions);
const prepareSparringStore = useSparringStore((state) => state.prepareForUser);
const prepareDebateStore = useDebateStore((state) => state.prepareForUser);
const resetSessionData = useSessionStore((state) => state.resetLocalData);
// A tool still writing for the previous account stops with its data.
const resetSessionStore = useCallback(() => {
abortToolRuns();
resetSessionData();
}, [resetSessionData]);
const clearSessionStore = useSessionStore((state) => state.clearSession);
const resetSettingsStore = useSettingsStore((state) => state.resetLocalData);
const resetSparringStore = useSparringStore((state) => state.resetLocalData);
const hydrateDebateStore = useDebateStore((state) => state.hydrate);
const resetDebateStore = useDebateStore((state) => state.resetLocalData);
const currentUserIdRef = useRef<string | undefined>(undefined);
useEffect(() => {
let mounted = true;
let hydratedUserId: string | undefined;
const confirmations = new Map<string, Promise<boolean>>();
const confirmRestoredSession = (accessToken: string): Promise<boolean> => {
const cached = confirmations.get(accessToken);
if (cached) return cached;
const pending = (async () => {
if (restoredSessionsBlocked()) return false;
if (typeof supabase.auth.getUser !== 'function') return true;
try {
// Bounded: during a sign-in callback nothing else ends the wait.
const { error } = await withAuthTimeout(supabase.auth.getUser(), 'User lookup timeout');
if (!isDeadUserLookup(error)) return true;
} catch {
// Auth did not answer. Keep the local session; this is an outage, not a dead login.
return true;
}
// The server refused this user. A sign-out that never answers must not hold startup either.
await withAuthTimeout(rejectDeadSession(supabase.auth), 'Sign-out timeout').catch((): void => undefined);
return false;
})();
confirmations.set(accessToken, pending);
return pending;
};
const dropRestoredSession = () => {
setActiveAccessToken(null);
setSession(null);
currentUserIdRef.current = undefined;
setJamUserId(undefined);
};
const hydrateStores = (userId?: string) => {
if (!userId || hydratedUserId === userId) return;
hydratedUserId = userId;
// Bind before any paint of protected pages. A full-page sign-in (magic
// link, Google) never sees the previous account's in-tab sign-out, so
// the persisted snapshot has to be checked against this user here.
prepareSessionStore(userId);
prepareSettingsStore(userId);
prepareSparringStore(userId);
prepareDebateStore(userId);
Promise.resolve(hydrateSessionStore()).catch((err) => console.warn('[Auth] hydrateSessionStore failed:', err));
Promise.resolve(hydrateSettingsStore()).catch((err) => console.warn('[Auth] hydrateSettingsStore failed:', err));
Promise.resolve(hydrateSparringStore()).catch((err) => console.warn('[Auth] hydrateSparringStore failed:', err));
Promise.resolve(hydrateDebateStore()).catch((err) => console.warn('[Auth] hydrateDebateStore failed:', err));
};
// Older clients persisted these credentials across browser sessions.
localStorage.removeItem('google_provider_token');
localStorage.removeItem('google_provider_refresh_token');
// Set before the first await: auth-js announces the empty stored session
// while the code is still being exchanged, and that must not end loading.
const callbackUrl = new URL(window.location.href);
let callbackPending = callbackUrl.pathname === AUTH_CALLBACK_PATH;
// True until the callback's code exchange answers or times out. auth-js
// announces sessions meanwhile (the stored account, then the exchange's
// own); only the exchange's answer may sign an account in, so the
// listener ignores them. After a timeout the user can act again, so the
// listener does too.
let exchangeInFlight = false;
let callbackTimedOut = false;
// The first account auth-js announced while the exchange ran: the one
// this browser had before the callback ('' for none).
let accountBeforeCallback: string | undefined;
const applySession = (session: Session, previousUserId = currentUserIdRef.current) => {
// What the listener does for any other sign-in.
clearCrossfireDraftOnAuthChange(previousUserId || undefined, session.user?.id);
if (session.provider_token) sessionStorage.setItem('google_provider_token', session.provider_token);
setActiveAccessToken(session.access_token, session.user.id);
setSession(session);
currentUserIdRef.current = session.user?.id;
setJamUserId(session.user?.id);
hydrateStores(session.user?.id);
};
// The session this browser already had, checked with the server first.
const restoreStoredSession = async () => {
// In some browser states (stalled locks, blocked storage, extensions),
// getSession() can hang long enough to look like a frozen app. If it
// doesn't resolve quickly, fail open to the login route and rely on
// onAuthStateChange to populate the session when available.
const getSessionResult = await Promise.race([
supabase.auth.getSession(),
new Promise<never>((_, reject) => setTimeout(() => reject(new Error('getSession timeout')), AUTH_OPERATION_TIMEOUT_MS)),
]);
if (!mounted) return;
const { data, error } = getSessionResult;
if (error) console.warn('[Auth] getSession error:', error.message);
const alive = data.session?.access_token ? await confirmRestoredSession(data.session.access_token) : true;
if (!mounted) return;
// Someone else signed in while this was checked: theirs stands, alive or not.
const current = currentUserIdRef.current;
if (current && current !== data.session?.user.id) return;
if (!alive) {
dropRestoredSession();
return;
}
if (data.session) {
applySession(data.session);
} else {
setActiveAccessToken(null);
setSession(null);
currentUserIdRef.current = undefined;
setJamUserId(undefined);
}
};
const hydrateOAuthCallbackSession = async (): Promise<Session | null> => {
if (!callbackPending) return null;
const { code, flowId, error } = readAuthCallbackParams(callbackUrl);
const cleanUrl = () => {
// A late answer can arrive after the user left the callback page.
if (window.location.pathname !== AUTH_CALLBACK_PATH) return;
window.history.replaceState(window.history.state, '', withoutCallbackParams(callbackUrl));
};
if (error || !code) {
cleanUrl();
if (error) setCallbackError(error);
return null;
}
// Finishing a sign-in is as explicit as pressing a sign-in button.
allowSessionsAfterSignIn();
// The flow id picks this sign-in's own verifier, so another tab's
// sign-in or a failed resend can't break the link.
exchangeInFlight = true;
const exchange = supabase.auth.exchangeCodeForSession(code, flowId ? { flowId } : undefined);
let settled = true;
try {
const { data, error: exchangeError } = await withAuthTimeout(exchange, 'OAuth code exchange timeout', OAUTH_CODE_EXCHANGE_TIMEOUT_MS);
exchangeInFlight = false;
if (exchangeError) throw exchangeError;
return data.session;
} catch (err) {
// A timeout is not a final answer: the code may still be good.
settled = !(err instanceof AuthTimeoutError);
exchangeInFlight = false;
callbackTimedOut = !settled;
// A session this browser already had still stands; init restores it.
console.warn('[Auth] sign-in callback failed:', err);
if (mounted) setCallbackError(settled ? authCallbackErrorFor(err) : 'timed-out');
if (!settled) {
// The late answer is final. A success is announced by auth-js like
// any sign-in; a failure gives its real reason and, if nobody signed
// in meanwhile, restores the stored session.
void exchange
.then(
({ data, error: lateError }): LateAnswer => (lateError || !data.session ? { session: null, lateError } : { session: data.session, lateError: null }),
(lateError: unknown): LateAnswer => ({ session: null, lateError }),
)
.then(async ({ session, lateError }) => {
if (!mounted) return;
const onCallbackPage = window.location.pathname === AUTH_CALLBACK_PATH;
cleanUrl();
if (session) {
setCallbackError(null);
allowSessionsAfterSignIn();
// The listener applies it, unless a blocked restore dropped it.
if (currentUserIdRef.current !== session.user.id) applySession(session);
return;
}
// Only the callback page still shows this attempt's outcome.
if (onCallbackPage) setCallbackError(authCallbackErrorFor(lateError));
if (!currentUserIdRef.current) await restoreStoredSession();
})
.catch((lateError: unknown) => console.warn('[Auth] restoring the stored session failed:', lateError));
}
return null;
} finally {
// Only once the exchange has an answer: a reload during the spinner,
// or after a timeout, must still find the unused code and its flow id.
if (mounted && settled) cleanUrl();
}
};
const init = async () => {
try {
const oauthSession = await hydrateOAuthCallbackSession();
if (!mounted) return;
if (oauthSession) {
applySession(oauthSession, accountBeforeCallback);
return;
}
// Timed out: nothing is restored while the exchange may still sign
// someone in. Its late answer, or the user, decides.
if (callbackTimedOut) return;
await restoreStoredSession();
} catch (err) {
console.warn('[Auth] init failed:', err);
if (!mounted) return;
setActiveAccessToken(null);
setSession(null);
currentUserIdRef.current = undefined;
setJamUserId(undefined);
} finally {
callbackPending = false;
if (mounted) setLoading(false);
}
};
void init();
const { data: authListener } = supabase.auth.onAuthStateChange((event, newSession) => {
if (import.meta.env.DEV) console.log('[Auth] Event:', event, 'Provider Token:', newSession?.provider_token ? 'present' : 'null');
if (exchangeInFlight) {
accountBeforeCallback ??= newSession?.user?.id ?? '';
return;
}
if (newSession && restoredSessionsBlocked()) {
dropRestoredSession();
if (!callbackPending) setLoading(false);
return;
}
const prevUserId = currentUserIdRef.current;
const nextUserId = newSession?.user?.id;
clearCrossfireDraftOnAuthChange(prevUserId, nextUserId);
currentUserIdRef.current = nextUserId;
setJamUserId(nextUserId);
setActiveAccessToken(newSession?.access_token ?? null, newSession?.user.id ?? null);
setSession(newSession);
// Provider access tokens are short-lived. Keep them only for this browser
// session; a later export must reauthorize if no fresh token is available.
if (newSession?.provider_token) {
if (import.meta.env.DEV) console.log('[Auth] Storing new provider_token');
sessionStorage.setItem('google_provider_token', newSession.provider_token);
}
if (!nextUserId) {
hydratedUserId = undefined;
resetSessionStore();
resetSettingsStore(!prevUserId);
resetSparringStore();
resetDebateStore();
useDebateContextStore.getState().resetLocalData();
clearGoogleTokens();
if (!callbackPending) setLoading(false);
return;
}
if (prevUserId && prevUserId !== nextUserId) {
hydratedUserId = undefined;
resetSessionStore();
resetSettingsStore();
resetSparringStore();
resetDebateStore();
useDebateContextStore.getState().resetLocalData();
}
// While a callback signs an account in, init checks and loads the
// outcome: the new account on success, the stored one only if the
// callback fails. Checking a stored account here could sign out, or
// block, the account being signed in.
if (callbackPending) return;
if (newSession?.access_token) {
void confirmRestoredSession(newSession.access_token).then((alive) => {
if (!mounted || !alive || restoredSessionsBlocked()) {
if (mounted && !alive) dropRestoredSession();
return;
}
hydrateStores(newSession.user?.id);
});
} else {
hydrateStores(nextUserId);
}
setLoading(false);
});
return () => {
mounted = false;
authListener.subscription.unsubscribe();
};
}, [hydrateDebateStore, hydrateSessionStore, hydrateSettingsStore, hydrateSparringStore, prepareDebateStore, prepareSessionStore, prepareSettingsStore, prepareSparringStore, resetDebateStore, resetSessionStore, resetSettingsStore, resetSparringStore]);
// sessionStorage holds this tab's Google token and sparring state.
const discardTabState = useCallback(() => {
clearGoogleTokens();
resetSparringStore();
}, [resetSparringStore]);
// The persisted stores are shared by both projects. Drop this project's
// stored session and clear every user-specific store so nothing crosses.
const discardLocalState = useCallback(() => {
localStorage.removeItem(AUTH_STORAGE_KEY);
localStorage.removeItem(`${AUTH_STORAGE_KEY}-user`);
discardTabState();
clearCrossfireDraftOnAuthChange(session?.user?.id, undefined);
resetSessionStore();
clearSessionStore();
resetSettingsStore(true);
resetDebateStore();
useDebateContextStore.getState().resetLocalData();
}, [session, clearSessionStore, discardTabState, resetDebateStore, resetSessionStore, resetSettingsStore]);
const value = useMemo<AuthContextValue>(() => ({
session,
user: session?.user ?? null,
loading,
callbackError,
clearCallbackError: () => setCallbackError(null),
signInWithGoogle: async ({ driveAccess = false, next } = {}) => {
allowSessionsAfterSignIn();
// Always use the SDK-generated URL so PKCE's code_verifier is written
// to storage before redirect. The previous Promise.race against a 1200ms
// timeout could fall back to a hand-built URL that skipped PKCE — Google
// would then call back to /app but the SDK couldn't exchange the code,
// leaving the user stuck on "Redirecting…" until manual refresh.
const { data, error } = await supabase.auth.signInWithOAuth({
provider: 'google',
options: {
redirectTo: authCallbackUrl(window.location.origin, next),
skipBrowserRedirect: true,
...(driveAccess ? { scopes: 'https://www.googleapis.com/auth/drive.file' } : {}),
queryParams: undefined,
},
});
if (error) throw error;
if (!data?.url) throw new Error('Google sign-in could not start. Please try again.');
await openExternalUrl(data.url);
},
signInWithEmailOtp: async (email, { next } = {}) => {
allowSessionsAfterSignIn();
const normalizedEmail = email.trim();
if (!normalizedEmail) throw new Error('Email is required.');
const { error } = await supabase.auth.signInWithOtp({
email: normalizedEmail,
// Prod's email also carries a link. It must open the callback, not the landing page.
options: { shouldCreateUser: true, emailRedirectTo: authCallbackUrl(window.location.origin, next) },
});
if (error) throw error;
},
verifyEmailOtp: async (email, token) => {
allowSessionsAfterSignIn();
const normalizedEmail = email.trim();
const normalizedToken = token.trim();
if (!normalizedEmail) throw new Error('Email is required.');
if (!normalizedToken) throw new Error('Verification code is required.');
const { error } = await supabase.auth.verifyOtp({
email: normalizedEmail,
token: normalizedToken,
type: 'email',
});
if (error) throw error;
},
signInWithDemoCode: async (code) => {
allowSessionsAfterSignIn();
const normalizedCode = code.trim().toUpperCase().replace(/\s+/g, '');
if (!normalizedCode) throw new Error('Demo code is required.');
const { data, error } = await supabase.functions.invoke('demo-code-login', {
body: {
code: normalizedCode,
browserFingerprint: getDemoBrowserFingerprint(),
},
});
if (error) throw await getFunctionError(error);
const handoff = parseDemoCodeHandoff(data);
const { error: verificationError } = await supabase.auth.verifyOtp({
token_hash: handoff.tokenHash,
type: 'magiclink',
});
if (verificationError) throw verificationError;
},
signOut: async () => {
markSignOutIntent();
try {
// Local scope: signing out here must not sign the account out on other devices.
await supabase.auth.signOut({ scope: 'local' });
} catch (error) {
clearSignOutIntent();
throw error;
}
clearCrossfireDraftOnAuthChange(session?.user?.id, undefined);
resetSessionStore();
resetSettingsStore();
resetSparringStore();
},
leaveEnvironment: async () => {
try {
// Local scope: switching backends must not sign the account out on other devices.
await withAuthTimeout(supabase.auth.signOut({ scope: 'local' }), 'Sign-out timed out');
} catch (error) {
console.warn('Sign-out before environment switch failed:', error);
}
// A hung sign-out never fires SIGNED_OUT, so clear local state directly.
discardLocalState();
},
discardTabState,
}), [session, loading, callbackError, discardLocalState, discardTabState, resetSessionStore, resetSettingsStore, resetSparringStore]);
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
}
export { useAuth };