2 Click New query, paste the code below, click Run
create table if not exists public.profiles (
id uuid references auth.users(id) on delete cascade primary key,
email text not null, full_name text, student_id text,
role text default 'student' check (role in ('student','instructor')),
last_seen timestamptz default now(), created_at timestamptz default now()
);
create table if not exists public.engagement (
id bigserial primary key,
user_id uuid references public.profiles(id) on delete cascade,
event text not null, lecture_id text, tab text, detail text,
ts timestamptz default now()
);
alter table public.profiles enable row level security;
alter table public.engagement enable row level security;
create policy "own_profile" on public.profiles for all using (auth.uid()=id) with check (auth.uid()=id);
create policy "own_eng" on public.engagement for all using (auth.uid()=user_id) with check (auth.uid()=user_id);
create policy "instr_profiles" on public.profiles for select using (exists(select 1 from public.profiles where id=auth.uid() and role='instructor'));
create policy "instr_eng" on public.engagement for select using (exists(select 1 from public.profiles where id=auth.uid() and role='instructor'));
create or replace function handle_new_user() returns trigger as $$
begin insert into public.profiles(id,email,full_name,student_id,role)
values(new.id,new.email,
coalesce(new.raw_user_meta_data->>'full_name',''),
coalesce(new.raw_user_meta_data->>'student_id',null),
case when new.email='abdo@qu.edu.qa' then 'instructor' else 'student' end
) on conflict(id) do nothing; return new; end; $$ language plpgsql security definer;
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 procedure handle_new_user();
3 Make yourself instructor — run this (replace email if needed):
UPDATE public.profiles SET role = 'instructor' WHERE email = 'abdo@qu.edu.qa';
✅ Done! Go to /dashboard to see student tracking.