diff --git a/migrations/1772922678763_initial-schema.sql b/migrations/1772922678763_initial-schema.sql index f4a5eb5..fc29367 100644 --- a/migrations/1772922678763_initial-schema.sql +++ b/migrations/1772922678763_initial-schema.sql @@ -90,7 +90,7 @@ CREATE INDEX idx_shared_links_token ON shared_links(share_token); CREATE OR REPLACE FUNCTION handle_new_user() RETURNS TRIGGER AS $$ BEGIN - INSERT INTO profiles (id, display_name, avatar_url) + INSERT INTO public.profiles (id, display_name, avatar_url) VALUES ( NEW.id, COALESCE(NEW.raw_user_meta_data->>'full_name', NEW.email), @@ -98,7 +98,7 @@ BEGIN ); RETURN NEW; END; -$$ LANGUAGE plpgsql SECURITY DEFINER; +$$ LANGUAGE plpgsql SECURITY DEFINER SET search_path = public; CREATE TRIGGER on_auth_user_created AFTER INSERT ON auth.users diff --git a/src/db/saved-simulations.ts b/src/db/saved-simulations.ts index 4bd54e9..0eed43c 100644 --- a/src/db/saved-simulations.ts +++ b/src/db/saved-simulations.ts @@ -1,4 +1,5 @@ import { supabase, isSupabaseConfigured } from '../auth/supabase-client'; +import { authStore } from '../auth/auth-store'; /** Row shape for the saved_simulations table */ export interface SavedSimulation { @@ -49,9 +50,13 @@ export async function saveSimulation(sim: { }): Promise { if (!isSupabaseConfigured()) return null; + const user = authStore.getState().user; + if (!user) return null; + const { data, error } = await supabase .from('saved_simulations') .insert({ + user_id: user.id, title: sim.title, description: sim.description || '', sim_type: sim.sim_type,