Spaces:
Running
Running
| -- Auto-sync trigger for new Supabase Auth users | |
| -- This ensures new users are automatically added to public.users table | |
| -- Run this in Supabase SQL Editor | |
| -- Create function to sync new auth users | |
| CREATE OR REPLACE FUNCTION public.handle_new_user() | |
| RETURNS TRIGGER AS $$ | |
| BEGIN | |
| INSERT INTO public.users (id, email, name, plan, created_at, updated_at) | |
| VALUES ( | |
| NEW.id, | |
| NEW.email, | |
| COALESCE(NEW.raw_user_meta_data->>'name', NEW.email), | |
| 'free', | |
| NEW.created_at, | |
| NEW.updated_at | |
| ) | |
| ON CONFLICT (id) DO NOTHING; | |
| RETURN NEW; | |
| END; | |
| $$ LANGUAGE plpgsql SECURITY DEFINER; | |
| -- Drop trigger if exists | |
| DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users; | |
| -- Create trigger on auth.users | |
| CREATE TRIGGER on_auth_user_created | |
| AFTER INSERT ON auth.users | |
| FOR EACH ROW | |
| EXECUTE FUNCTION public.handle_new_user(); | |
| SELECT 'Auto-sync trigger created successfully!' as status; | |