Access Control
Role-based access control (RBAC), middleware enforcement, and route protection.
The Care Nexus uses a multi-layer Role-Based Access Control (RBAC) system. Access is enforced at three levels: the API middleware layer, frontend route guards, and database query scoping. This defense-in-depth model ensures security even if one layer is bypassed.
Roles
| Role | Key Permissions | Cannot Access |
|---|---|---|
| patient | Own records, appointments, family data, AI chat | Other patients' data, doctor management, clinic analytics |
| doctor | Own patients, prescriptions, schedule, revenue | Other doctors' patients, clinic staff management |
| clinic_admin | All clinic doctors, staff, appointments, analytics | Other clinics' data, platform-wide users |
Middleware Stack
authMiddleware
Every protected API route passes through authMiddleware which validates the JWT token. If invalid, expired, or missing, it returns 401 Unauthorized before reaching the controller. The decoded payload (userId, role) is attached to req.user.
roleMiddleware
roleMiddleware ensures that the user’s role matches the required role for the endpoint. For example, all /api/clinic/* routes require clinic_admin. Unauthorized access results in 403 Forbidden.
Frontend Route Guards
Layout components (DoctorLayout, PatientLayout, ClinicLayout) check the user role from localStorage on mount. If mismatched, the user is redirected to /login, preventing access to unauthorized dashboards.
Never trust the client
Frontend guards improve UX but do not provide security. All authorization is enforced on the backend for every request.