Files
Convivo/supabase/migrations/20260823000000_admin_app.sql
32c86767ef Phase 2: Admin console (admin.appe.at)
A desktop-first React app for organizers, guarded to the admin role.

Backend (supabase/migrations/20260823000000_admin_app.sql):
- Admin-only SELECT policies on preferences, connections, feedback
- reset_qr_token RPC: regenerates a lanyard token, re-hashes it as the
  Auth password (old token stops working), and clears the attendee's
  sessions to force re-login

App (apps/admin, port 3001):
- Login: same QR+email flow, but non-admin sessions are rejected
- Dashboard: live stats + top points + recent activity
- Attendees: search, edit name/company/role, reset QR, adjust points
- Schedule: manage slots + sessions (create/edit/delete)
- Activity: check-ins / connections / feedback tables
- Same-origin proxy (Vite dev / nginx in Docker), same design tokens

Docker + scripts:
- convivo-admin service in docker-compose.yml (:3001)
- admin:dev / admin:build root scripts
- fixed db:reset script (supabase reset -> supabase db reset)

Docs: new frontend/admin-app.md; updated architecture, rls, rpcs,
docker, next-steps, and READMEs.

Verified: build clean, all views + flows browser-tested, Docker build +
proxy + login + admin guard confirmed. DB reset to clean seed.

Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
2026-08-22 19:47:22 +02:00

80 lines
3.4 KiB
PL/PgSQL

-- =============================================================================
-- Convivo — Phase 2 (Admin app): admin read access + QR reset RPC
--
-- The admin app (apps/admin) needs to:
-- 1. Read ALL profiles, preferences, connections, feedback, checkins,
-- points_log (a full event overview), not just the admin's own rows.
-- 2. Reset an attendee's QR token (which doubles as their auth password),
-- forcing them to re-login with the new lanyard code.
--
-- This migration adds the missing admin SELECT policies and a single
-- admin-only RPC. It does NOT change any existing policy.
-- =============================================================================
-- -----------------------------------------------------------------------------
-- ADMIN READ POLICIES
-- (profiles, checkins, points_log, leaderboard already allow admin/all reads.)
-- -----------------------------------------------------------------------------
-- preferences: admin can read everyone's picks
create policy "prefs_select_admin" on public.preferences for select to authenticated
using (public.is_admin());
-- connections: admin can read all connections (event overview)
create policy "conn_select_admin" on public.connections for select to authenticated
using (public.is_admin());
-- feedback: admin can read all feedback
create policy "feedback_select_admin" on public.feedback for select to authenticated
using (public.is_admin());
-- -----------------------------------------------------------------------------
-- reset_qr_token(p_profile_id) — admin only
--
-- Regenerates the attendee's QR token, syncs it into auth.users as the new
-- bcrypt password (the QR token IS the login password), and clears their
-- existing sessions so they must re-login with the new code.
--
-- security definer: runs as the table owner (postgres), which has write access
-- to both public.profiles and auth.* (verified).
-- -----------------------------------------------------------------------------
create or replace function public.reset_qr_token(p_profile_id uuid)
returns table (profile_id uuid, qr_token text)
language plpgsql security definer set search_path = public as $$
declare
v_uid uuid := auth.uid();
v_token text;
v_salt text;
begin
if v_uid is null then raise exception 'not authenticated'; end if;
if not public.is_admin() then raise exception 'admin only'; end if;
if not exists (select 1 from public.profiles where id = p_profile_id) then
raise exception 'profile not found';
end if;
-- Generate a fresh, human-friendly token: CVV-XXX-NNNN
v_token := 'CVV-' || upper(substr(md5(random()::text), 1, 3)) || '-' ||
lpad((floor(random() * 10000))::int::text, 4, '0');
-- Update the profile (source of truth for the lanyard).
update public.profiles set qr_token = v_token where id = p_profile_id;
-- Sync the auth password so the new token works for login.
-- (pgcrypto's gen_salt/crypt live in the `extensions` schema — qualify them,
-- since this function's search_path is pinned to `public`.)
v_salt := extensions.gen_salt('bf');
update auth.users
set encrypted_password = extensions.crypt(v_token, v_salt),
updated_at = now()
where id = p_profile_id;
-- Force re-login: drop the user's existing sessions.
delete from auth.sessions where user_id = p_profile_id;
return query select p_profile_id, v_token;
end;
$$;
grant execute on function public.reset_qr_token(uuid) to authenticated;