-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriggers_migration.sql
More file actions
28 lines (27 loc) · 877 Bytes
/
Copy pathtriggers_migration.sql
File metadata and controls
28 lines (27 loc) · 877 Bytes
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
-- FUNCTION: handle_new_user
-- Automatically creates a profile in 'verifylive_profiles' when a new user signs up via Auth.
create or replace function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = public
as $$
begin
insert into public.verifylive_profiles (id, full_name, avatar_url, username)
values (
new.id,
new.raw_user_meta_data ->> 'full_name',
new.raw_user_meta_data ->> 'avatar_url',
-- Generate a default username if none is provided, fallback to email part
coalesce(
new.raw_user_meta_data ->> 'username',
split_part(new.email, '@', 1)
)
);
return new;
end;
$$;
-- TRIGGER: on_auth_user_created
-- Binds the function to the auth.users table
create or replace trigger on_auth_user_created
after insert on auth.users
for each row execute procedure public.handle_new_user();