Search code examples
supabase

Firebase function equivalent in Supabase


I want to be able to write a function in TypeScript and have it run when a user is created (probably when an insert event occurs on the auth table)

I don’t want to author this in plpgsql

How would I do this?


Solution

  • There are two possible approaches:

    • You can create a trigger that calls an edge function.
    • You can create the function using PLV8 (Javascript).
    
    CREATE OR REPLACE FUNCTION public.telegram_on_new_user() 
    RETURNS trigger 
    security definer 
    set search_path public, auth, extensions 
    LANGUAGE plpgsql 
    AS $$ 
    BEGIN 
    -- Passing the email address to the function
    PERFORM http_get('https://xxxx.functions.supabase.co/telegram-bot?email=' || new.email);
    RETURN NEW; 
    END; 
    $$; 
    
    CREATE TRIGGER new_user_trigger 
    AFTER INSERT ON auth.users 
    FOR EACH ROW EXECUTE PROCEDURE public.telegram_on_new_user(); 
    

    You can read more about how to create webhooks in Supabase in my blog.