Spaces:
Running
Running
File size: 1,329 Bytes
6fc3143 |
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 35 36 37 38 39 40 41 42 43 |
-- Create a table for public profiles
create table if not exists public.users (
id uuid references auth.users on delete cascade not null primary key,
email text,
full_name text,
avatar_url text,
credits integer default 5,
created_at timestamp with time zone default timezone('utc'::text, now()) not null
);
-- Set up Row Level Security (RLS)
alter table public.users enable row level security;
create policy "Public profiles are viewable by everyone." on public.users
for select using (true);
create policy "Users can insert their own profile." on public.users
for insert with check (auth.uid() = id);
create policy "Users can update own profile." on public.users
for update using (auth.uid() = id);
-- Create a function to handle new user signup
create or replace function public.handle_new_user()
returns trigger as $$
begin
insert into public.users (id, email, full_name, avatar_url, credits)
values (
new.id,
new.email,
new.raw_user_meta_data->>'full_name',
new.raw_user_meta_data->>'avatar_url',
5 -- Default credits
);
return new;
end;
$$ language plpgsql security definer;
-- Create a trigger to call the function on new user creation
create or replace trigger on_auth_user_created
after insert on auth.users
for each row execute procedure public.handle_new_user();
|