Skip to content

Commit fa208e4

Browse files
committed
fix: CORS for Google Docs export and add debug logging
- Allow localhost (any port), 127.0.0.1, and 192.168.x.x origins - Add console logging for auth token debugging - The token IS working (confirmed locally) - issue was CORS
1 parent 5616069 commit fa208e4

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎src/lib/export-utils.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,14 @@ async function callGoogleDocsEdgeFunction(
438438
// Try session.provider_token first, fallback to localStorage
439439
// Supabase only provides provider_token immediately after OAuth, not on session refresh
440440
let providerToken = session.provider_token;
441+
const tokenSource = providerToken ? 'session' : 'localStorage';
441442
if (!providerToken) {
442443
providerToken = localStorage.getItem('google_provider_token');
443444
}
444445

446+
console.log('[GDocs Export] Token source:', tokenSource, 'Token present:', !!providerToken,
447+
'Token preview:', providerToken ? `${providerToken.substring(0, 10)}...` : 'none');
448+
445449
if (!providerToken) {
446450
return {
447451
success: false,

‎src/lib/supabase/AuthProvider.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
3939

4040
init();
4141

42-
const { data: authListener } = supabase.auth.onAuthStateChange((_event, newSession) => {
42+
const { data: authListener } = supabase.auth.onAuthStateChange((event, newSession) => {
43+
console.log('[Auth] Event:', event, 'Provider Token:', newSession?.provider_token ? 'present' : 'null');
44+
4345
const prevUserId = session?.user?.id;
4446
const nextUserId = newSession?.user?.id;
4547
setSession(newSession);
@@ -48,6 +50,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
4850
// Persist Google provider_token for later use (e.g., Google Docs export)
4951
// This token is only available immediately after OAuth, not on session refresh
5052
if (newSession?.provider_token) {
53+
console.log('[Auth] Storing new provider_token');
5154
localStorage.setItem('google_provider_token', newSession.provider_token);
5255
}
5356

‎supabase/functions/export-gdoc/index.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55
const ALLOWED_ORIGINS = [
66
"https://debate.pi-stack.org",
77
"http://localhost:5173",
8+
"http://localhost:8080",
89
"http://127.0.0.1:54321"
910
];
1011

1112
function getCorsHeaders(origin: string | null) {
12-
const allowedOrigin = origin && ALLOWED_ORIGINS.includes(origin) ? origin : ALLOWED_ORIGINS[0];
13+
// Allow any localhost/127.0.0.1 origin for development
14+
// Also allow local network IPs (192.168.x.x) for mobile/device testing
15+
const isDevOrigin = origin && (
16+
origin.startsWith('http://localhost') ||
17+
origin.startsWith('http://127.0.0.1') ||
18+
origin.startsWith('http://192.168.')
19+
);
20+
const allowedOrigin = isDevOrigin || (origin && ALLOWED_ORIGINS.includes(origin))
21+
? origin
22+
: ALLOWED_ORIGINS[0];
1323
return {
1424
"Access-Control-Allow-Origin": allowedOrigin,
1525
"Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type",

0 commit comments

Comments
 (0)