Two-Tier User System
The Medical Center API implements a dual user architecture that separates authenticated internal staff from unauthenticated public clients.Internal Users (Authenticated)
Personas + Usuarios → Staff members who authenticate via Supabase and have system access.- Admins (
admin) - Assistants (
asistente) - Doctors (linked to
Personasbut not authenticated)
Public Users (Unauthenticated)
Clientes_publicos → External users who submit appointment requests without authentication.- Created automatically when booking appointments
- No login credentials
- No system access
This separation ensures that public appointment booking is frictionless while maintaining strict access control for internal operations.
Role Definitions
Admin Role
Admin Capabilities
Admin Capabilities
User Management
- Create, update, and delete internal users (
/api/usuarios) - Assign roles to users
- View all user profiles
- Create, update, and delete doctor profiles (
/api/medicos) - Manage doctor specialties
- Activate/deactivate doctors
- View all appointments (
/api/citas) - Confirm and assign appointments to doctors
- Cancel appointments
- Mark appointments as attended
- Delete appointments (admin-only)
- View and search public clients
- Update client information
- Manage specialties
- Access all dashboard statistics
Asistente Role
Asistente Capabilities
Asistente Capabilities
Appointment Management
- View all appointments (
/api/citas) - Confirm and assign appointments to doctors
- Cancel appointments
- Mark appointments as attended
- Search appointments by patient name
- View public client information
- Update client details
- View doctor list (cannot modify)
- View their own profile (
/api/usuarios/me)
- ❌ Cannot create or delete users
- ❌ Cannot manage doctors
- ❌ Cannot delete appointments
- ❌ Cannot modify system configuration
Doctor (No Authentication)
Doctors are NOT authenticated users. They exist in the system asMedicos records but don’t have login credentials.
Authentication Implementation
The API uses Supabase Authentication with custom middleware for role checking.Auth Middleware
Verifies JWT tokens and loads user data:The middleware only decodes the JWT (doesn’t verify signature) because Supabase has already validated the token. It then checks if the user exists in the internal
usuarios table.Role Middleware
Enforces role-based access control:Route Protection Examples
Admin-Only Endpoint
Admin + Asistente Endpoint
Public Endpoint (No Auth)
Role-Based Access Matrix
| Resource | Admin | Asistente | Public |
|---|---|---|---|
| Appointments | |||
| Create request | ✅ | ✅ | ✅ |
| View all | ✅ | ✅ | ❌ |
| Confirm/Assign | ✅ | ✅ | ❌ |
| Cancel | ✅ | ✅ | ❌ |
| Mark attended | ✅ | ✅ | ❌ |
| Delete | ✅ | ❌ | ❌ |
| Doctors | |||
| View list (public) | ✅ | ✅ | ✅ |
| View full details | ✅ | ✅ | ❌ |
| Create | ✅ | ❌ | ❌ |
| Update | ✅ | ❌ | ❌ |
| Delete | ✅ | ❌ | ❌ |
| Users | |||
| Create | ✅ | ❌ | ❌ |
| List all | ✅ | ❌ | ❌ |
| View own profile | ✅ | ✅ | ❌ |
| Update roles | ✅ | ❌ | ❌ |
| Delete | ✅ | ❌ | ❌ |
| Clients | |||
| Create (auto) | ✅ | ✅ | ✅ |
| View | ✅ | ✅ | ❌ |
| Update | ✅ | ✅ | ❌ |
User Creation Flow
How to Create an Internal User
How to Create an Internal User
- Admin creates persona via
/api/personaswith personal details - User signs up in Supabase (gets UUID)
- Admin creates usuario via
/api/usuarioslinking:- Supabase UUID (
id) - Persona ID (
persona_id) - Role (
role:adminorasistente)
- Supabase UUID (
Retrieving User Role
On Login
After Supabase authentication, the client fetches the user’s role:This endpoint is not protected by
authMiddleware because it’s called immediately after login to determine the user’s role.For Current User
Authenticated users can fetch their complete profile:Best Practices
Role Assignment
- Only admins can create and modify user roles
- Role changes take effect immediately (no re-login required)
- A user’s role is tied to their UUID from Supabase