-- Execute no Supabase: SQL Editor > New query > Run create table if not exists public.profiles ( id uuid primary key references auth.users(id) on delete cascade, email text, display_name text, birth_date date, bio text default '', is_pro boolean not null default false, provider text, created_at timestamptz not null default now() ); alter table public.profiles enable row level security; create policy "ler proprio perfil" on public.profiles for select using (auth.uid() = id); create policy "atualizar proprio perfil" on public.profiles for update using (auth.uid() = id) with check (auth.uid() = id); -- o utilizador só pode alterar nome e bio (nunca is_pro) revoke update on public.profiles from anon, authenticated; grant update (display_name, bio) on public.profiles to authenticated; -- cria o perfil automaticamente em qualquer registo (e-mail, Google, Discord) create or replace function public.handle_new_user() returns trigger language plpgsql security definer set search_path = public as $$ begin insert into public.profiles (id, email, display_name, birth_date, provider) values ( new.id, new.email, coalesce(new.raw_user_meta_data->>'display_name', new.raw_user_meta_data->>'full_name', new.raw_user_meta_data->>'name', split_part(coalesce(new.email, 'utilizador@nexora'), '@', 1)), nullif(new.raw_user_meta_data->>'birth_date', '')::date, coalesce(new.raw_app_meta_data->>'provider', 'email') ); return new; end $$; drop trigger if exists on_auth_user_created on auth.users; create trigger on_auth_user_created after insert on auth.users for each row execute function public.handle_new_user();