feat: implement Supabase database schema and core authentication flow with registration and login pages
This commit is contained in:
@@ -121,3 +121,62 @@ create policy "Users can delete their own chat messages." on public.chat_message
|
||||
and chat_sessions.user_id = auth.uid()
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
-- 5. Create profiles table
|
||||
create table public.profiles (
|
||||
id uuid references auth.users(id) on delete cascade primary key,
|
||||
first_name text,
|
||||
last_name text,
|
||||
avatar_url text,
|
||||
updated_at timestamp with time zone default timezone('utc'::text, now()) not null
|
||||
);
|
||||
|
||||
alter table public.profiles enable row level security;
|
||||
|
||||
create policy "Users can view their own profile." on public.profiles
|
||||
for select using (auth.uid() = id);
|
||||
create policy "Users can insert their own profile." on public.profiles
|
||||
for insert with check (auth.uid() = id);
|
||||
create policy "Users can update their own profile." on public.profiles
|
||||
for update using (auth.uid() = id);
|
||||
|
||||
-- Function to handle new user signup
|
||||
create or replace function public.handle_new_user()
|
||||
returns trigger as $$$
|
||||
begin
|
||||
insert into public.profiles (id, first_name, last_name, avatar_url)
|
||||
values (new.id, '', '', '');
|
||||
return new;
|
||||
end;
|
||||
$$$ language plpgsql security definer;
|
||||
|
||||
-- Drop trigger if exists to avoid errors on multiple runs
|
||||
drop trigger if exists on_auth_user_created on auth.users;
|
||||
|
||||
-- Trigger to automatically create profile on signup
|
||||
create trigger on_auth_user_created
|
||||
after insert on auth.users
|
||||
for each row execute procedure public.handle_new_user();
|
||||
|
||||
-- Setup storage bucket for avatars
|
||||
insert into storage.buckets (id, name, public)
|
||||
values ('avatars', 'avatars', true)
|
||||
on conflict (id) do nothing;
|
||||
|
||||
create policy "Avatar images are publicly accessible."
|
||||
on storage.objects for select
|
||||
using ( bucket_id = 'avatars' );
|
||||
|
||||
create policy "Users can upload an avatar."
|
||||
on storage.objects for insert
|
||||
with check ( bucket_id = 'avatars' AND auth.uid()::text = (storage.foldername(name))[1] );
|
||||
|
||||
create policy "Users can update their own avatar."
|
||||
on storage.objects for update
|
||||
using ( bucket_id = 'avatars' AND auth.uid()::text = (storage.foldername(name))[1] );
|
||||
|
||||
create policy "Users can delete their own avatar."
|
||||
on storage.objects for delete
|
||||
using ( bucket_id = 'avatars' AND auth.uid()::text = (storage.foldername(name))[1] );
|
||||
|
||||
|
||||
Reference in New Issue
Block a user