Spaces:
Running
Running
File size: 943 Bytes
8a440fa | 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 | -- 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;
|