1. Setup Environment Supabase

Tambahkan pada file .env (frontend):

VITE_SUPABASE_URL=https://zursrnbabevivnpzuuvc.supabase.co
VITE_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...


2. Buat Schema Profile

2.1 Tabel profiles

create table public.profiles (
  id uuid not null,
  full_name text null,
  email text null,
  role text not null default 'admin'::text,
  avatar_url text null,
  created_at timestamp with time zone null default now(),
  updated_at timestamp with time zone null default now(),
  gender text null,
  phone text null,
  address text null,
  note text null,
  employee_code text null,
  created_by uuid null,
  constraint profiles_pkey primary key (id),
  constraint profiles_created_by_fkey foreign KEY (created_by) references auth.users (id) on delete set null,
  constraint profiles_id_fkey foreign KEY (id) references auth.users (id) on delete CASCADE
) TABLESPACE pg_default;

2.2 Trigger: otomatis bikin profile setelah user signup

CREATE OR REPLACE FUNCTION public.handle_new_auth_user()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
  INSERT INTO public.profiles (id, email)
  VALUES (NEW.id, NEW.email);

  RETURN NEW;
END;
$$;

3. Aktifkan Row Level Security

alter table public.profiles enable row level security;


JANGAN PAKAI TABEL VIEW UNTUK PROFILE

1️⃣ Drop view

drop view if exists public.view_profiles cascade;

2️⃣ Gantikan dengan function:

create or replace function public.get_view_profiles()
returns setof public.profiles
language sql
security invoker
set row_security = on
as $$
  select *
  from public.profiles;
$$;


4. Policy di profiles