|
1 | 1 | import { createClient } from "@supabase/supabase-js" |
2 | 2 |
|
| 3 | +console.log("Supabase URL:", import.meta.env.VITE_SUPABASE_URL) |
| 4 | +console.log( |
| 5 | + "Supabase Anon Key:", |
| 6 | + import.meta.env.VITE_SUPABASE_ANON_KEY?.slice(0, 10) + "..." |
| 7 | +) |
| 8 | + |
3 | 9 | const supabaseUrl = import.meta.env.VITE_SUPABASE_URL |
4 | 10 | const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY |
5 | 11 |
|
| 12 | +if (!supabaseUrl || !supabaseAnonKey) { |
| 13 | + throw new Error("Missing Supabase environment variables") |
| 14 | +} |
| 15 | + |
6 | 16 | export const supabase = createClient(supabaseUrl, supabaseAnonKey, { |
7 | 17 | auth: { |
8 | 18 | flowType: "pkce", |
9 | | - detectSessionInUrl: true |
| 19 | + detectSessionInUrl: true, |
| 20 | + persistSession: true, |
| 21 | + autoRefreshToken: true, |
| 22 | + debug: true, |
| 23 | + storage: { |
| 24 | + getItem: (key) => { |
| 25 | + const item = localStorage.getItem(key) |
| 26 | + console.log("Auth storage - Getting key:", key, "Value exists:", !!item) |
| 27 | + return item |
| 28 | + }, |
| 29 | + setItem: (key, value) => { |
| 30 | + console.log("Auth storage - Setting key:", key) |
| 31 | + localStorage.setItem(key, value) |
| 32 | + }, |
| 33 | + removeItem: (key) => { |
| 34 | + console.log("Auth storage - Removing key:", key) |
| 35 | + localStorage.removeItem(key) |
| 36 | + } |
| 37 | + } |
| 38 | + }, |
| 39 | + realtime: { |
| 40 | + params: { |
| 41 | + eventsPerSecond: 10 |
| 42 | + }, |
| 43 | + headers: { |
| 44 | + apikey: supabaseAnonKey |
| 45 | + } |
| 46 | + } |
| 47 | +}) |
| 48 | + |
| 49 | +export const signInWithGoogle = async () => { |
| 50 | + try { |
| 51 | + console.log("Initiating Google sign in...") |
| 52 | + const { data, error } = await supabase.auth.signInWithOAuth({ |
| 53 | + provider: "google", |
| 54 | + options: { |
| 55 | + redirectTo: window.location.origin |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + if (error) { |
| 60 | + console.error("Google sign in error:", error) |
| 61 | + throw error |
| 62 | + } |
| 63 | + |
| 64 | + console.log("Google sign in response:", data) |
| 65 | + return data |
| 66 | + } catch (error) { |
| 67 | + console.error("Unexpected error during Google sign in:", error) |
| 68 | + throw error |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +let channel: ReturnType<typeof supabase.channel> | null = null |
| 73 | + |
| 74 | +// Monitor auth state changes and manage realtime connection |
| 75 | +supabase.auth.onAuthStateChange((event, session) => { |
| 76 | + console.log("Auth state changed:", event, session?.user?.id) |
| 77 | + console.log("Full session data:", session) |
| 78 | + |
| 79 | + if (event === "SIGNED_IN" && session) { |
| 80 | + // Only establish realtime connection after successful sign in |
| 81 | + console.log("Establishing realtime connection...") |
| 82 | + |
| 83 | + // Clean up existing channel if any |
| 84 | + if (channel) { |
| 85 | + channel.unsubscribe() |
| 86 | + } |
| 87 | + |
| 88 | + channel = supabase.channel("system", { |
| 89 | + config: { |
| 90 | + presence: { |
| 91 | + key: session.user.id |
| 92 | + } |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + channel |
| 97 | + .on("system", { event: "*" }, (payload) => { |
| 98 | + console.log("System event:", payload) |
| 99 | + }) |
| 100 | + .subscribe((status) => { |
| 101 | + console.log("Realtime subscription status:", status) |
| 102 | + if (status === "SUBSCRIBED") { |
| 103 | + console.log("Successfully connected to realtime system") |
| 104 | + } |
| 105 | + if (status === "CHANNEL_ERROR") { |
| 106 | + console.error("Realtime connection error - will retry in 5s") |
| 107 | + setTimeout(() => { |
| 108 | + channel?.subscribe() |
| 109 | + }, 5000) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + if (event === "SIGNED_OUT") { |
| 115 | + // Clean up realtime connection on sign out |
| 116 | + if (channel) { |
| 117 | + console.log("Cleaning up realtime connection") |
| 118 | + channel.unsubscribe() |
| 119 | + channel = null |
| 120 | + } |
10 | 121 | } |
11 | 122 | }) |
0 commit comments