Overview
The Medical Center API uses Supabase JWT (JSON Web Token) authentication to secure admin and assistant endpoints. Public endpoints like appointment creation and viewing specialties don’t require authentication.Authentication Architecture
The API implements a two-tier authentication system:- Supabase Auth: Handles user authentication and JWT token generation
- Internal User Management: Maps Supabase users to internal roles (admin/assistant)
How It Works
User Roles
The API supports two user roles:| Role | Permissions | Use Case |
|---|---|---|
| admin | Full access to all endpoints including user creation, doctor management, and appointment operations | System administrators |
| asistente | Can manage appointments, view doctors, and access patient records | Front desk staff |
Authentication Middleware
The API uses middleware to protect endpoints. Here’s how it works under the hood:src/middleware/auth.middleware.js
Role Middleware
After authentication, endpoints can require specific roles:src/middleware/role.middleware.js
Getting Started with Authentication
Step 1: Sign In to Supabase
Authenticate with Supabase to obtain a JWT token:Step 2: Make Authenticated Requests
Use the access token in theAuthorization header:
Creating Admin Users
Only admins can create new authenticated users. The process involves two steps:- Create a Supabase auth user
- Create an internal user record with role assignment
Create Supabase Auth User (Admin Only)
The
auth_user_id returned is the UUID you’ll use when creating the internal user record.Create Internal User Record
After creating the Supabase auth user, create the internal user with role assignment:Protected Endpoint Examples
Admin-Only Endpoints
These endpoints require theadmin role:
POST /api/especialidades- Create specialtyPUT /api/especialidades/:id- Update specialtyDELETE /api/especialidades/:id- Delete specialtyPOST /api/medicos- Create doctorDELETE /api/citas/:id- Delete appointmentPOST /api/admin-auth/crear-auth- Create auth user
Admin & Assistant Endpoints
These endpoints accept bothadmin and asistente roles:
GET /api/citas- List all appointmentsGET /api/citas/:id- Get appointment by IDPUT /api/citas/:id/confirmar- Confirm appointmentPUT /api/citas/:id/cancelar- Cancel appointmentPUT /api/citas/:id/atender- Mark as attended
Error Responses
The API returns specific error codes for authentication issues:| Status Code | Message | Meaning |
|---|---|---|
401 | Token requerido | No Authorization header provided |
401 | Token inválido | JWT token is malformed or expired |
403 | Usuario no autorizado | User exists in Supabase but not in internal database |
403 | Acceso denegado | User doesn’t have the required role |
Security Best Practices
-
Never expose your Service Role Key: The
SUPABASE_SERVICE_ROLE_KEYbypasses Row Level Security. Only use it server-side for admin operations. - Use HTTPS in production: Always use HTTPS to prevent token interception.
- Token expiration: JWT tokens expire after 1 hour by default. Implement token refresh logic in your client application.
- Environment variables: Store all secrets in environment variables, never commit them to version control.
- Rate limiting: Implement rate limiting on authentication endpoints to prevent brute force attacks.
Token Refresh
Supabase access tokens expire after 1 hour. Use the refresh token to obtain a new access token:Next Steps
API Reference
Explore all available endpoints
Appointments API
Learn about appointment management