Created
April 30, 2022 15:18
-
-
Save kevcodez/2d0fac36e3e3dbfea42a137fd6c427a3 to your computer and use it in GitHub Desktop.
Supabase Before insert hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Create a new trigger | |
CREATE OR REPLACE FUNCTION public.notify_parqet_api_signup() | |
RETURNS TRIGGER AS $$ | |
DECLARE | |
parqet_user_id varchar; | |
social_provider_id varchar; | |
BEGIN | |
-- Extract ID from social provider, can be null | |
social_provider_id := NEW.raw_user_meta_data->>'provider_id'; | |
-- Send blocking request to API to determine user (backwards compatible with Auth0) | |
parqet_user_id := (SELECT content::jsonb->>'_id' from extensions.http(( | |
'POST', | |
'<api-url>/v1/users/create', | |
ARRAY[extensions.http_header('some-security-header', '<token>')], | |
'application/json', | |
jsonb_build_object('email', new.email, 'userId', new.id::text, 'loginMethod', NEW.raw_app_meta_data->>'provider', 'socialProviderId', social_provider_id)::text | |
)::extensions.http_request)); | |
-- Attach Parqet user Id to app metadata | |
NEW.raw_app_meta_data:=NEW.raw_app_meta_data || jsonb_build_object('parqetUserId', parqet_user_id); | |
RETURN NEW; | |
END; | |
$$ | |
-- Run trigger BEFORE an insert on the auth.users table | |
CREATE TRIGGER notify_parqet_api_signup_tr | |
BEFORE INSERT ON auth.users | |
FOR EACH ROW EXECUTE PROCEDURE public.notify_parqet_api_signup(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment