Overview
The OdontologyApp API uses cookie-based session authentication. After successfully logging in, the server sets an HTTP-only session cookie that is automatically included in subsequent requests.Authentication Flow
- Client sends credentials to
/api/auth/login - Server validates credentials against the database
- Server creates a session and sets a cookie
- Client includes the cookie in all subsequent requests
- Server validates the session on each request via
hooks.server.js
Login Endpoint
To authenticate, send a POST request to the login endpoint:Request Body
The user’s username
The user’s password (will be compared with bcrypt hash)
Example Request
Success Response (200)
Indicates successful authentication
User session data stored in the cookie
User’s unique identifier
User’s full name
User’s email address
User’s role (admin, doctor, receptionist, user)
User’s initials for display
User’s preferred theme (light/dark)
Associated clinic branch name
URL to user’s avatar image
Error Responses
400 Bad Request - Missing Credentials
400 Bad Request - Missing Credentials
401 Unauthorized - User Not Found
401 Unauthorized - User Not Found
401 Unauthorized - Incorrect Password
401 Unauthorized - Incorrect Password
500 Server Error
500 Server Error
Session Cookie
Upon successful login, the server sets a session cookie with the following properties:Session Data Structure
The session cookie contains JSON-encoded user information:Authentication Middleware
The API uses a server hook (src/hooks.server.js) to validate sessions on every request:
How It Works
- Session Parsing: The hook reads the
sessioncookie and parses the JSON data - User Injection: Valid session data is injected into
event.locals.user - Route Protection: Non-authenticated users are redirected to
/login(except for/apiroutes) - Role-Based Access: Admin-only routes are protected based on the user’s role
Protected Routes
The following routes require admin role:/admin/settings/admin/treatments/admin/reports/users/branches/logs
API Route Behavior
API routes (/api/*) are not automatically redirected. Instead, each endpoint checks for authentication:
Making Authenticated Requests
After logging in, include the session cookie in all API requests:Using cURL
Using JavaScript (Browser)
Using JavaScript (Node.js)
Permission System
Beyond authentication, most endpoints require specific permissions. The system uses three levels:1. Admin Role
Users withrole: "admin" automatically have access to all endpoints and permissions.
2. Permission-Based Access
Other users must have specific permission codes assigned. Common permissions include:VIEW_PATIENTS- View patient list and detailsCREATE_PATIENTS- Create new patientsEDIT_PATIENTS- Modify patient informationDELETE_PATIENTS- Delete patientsVIEW_APPOINTMENTS- View appointmentsCREATE_APPOINTMENTS- Create appointmentsMANAGE_INVENTORY- Manage inventory itemsVIEW_INVENTORY- View inventory
3. Permission Resolution
ThecheckPermission() function resolves permissions in this order:
- Admin check: If user is admin, return
true - Database check: Query
sp_check_single_permission(user_id, permission_code) - Static fallback: Check
ROLE_PERMISSIONSobject for role-based defaults
Example Permission Check
Forbidden Response
When a user lacks the required permission, the API returns:403 Forbidden
Logout Endpoint
To end a session, send a DELETE request to the login endpoint:Example Request
Response (200)
Session Expiration
Sessions automatically expire after 24 hours (86400 seconds). After expiration:- The cookie is no longer valid
- API requests return
401 Unauthorized - User must log in again to obtain a new session
Security Considerations
Password Security
- Passwords are hashed using bcrypt before storage
- The API never returns password hashes in responses
- Password comparison uses
bcrypt.compare()for timing-attack resistance
Cookie Security
- HttpOnly: Prevents JavaScript access (XSS protection)
- SameSite: lax: Prevents CSRF attacks
- Path: /: Cookie available to all routes
- Secure: Should be
truein production with HTTPS
Login Logging
Successful logins are logged to the database:Troubleshooting
401 Unauthorized on authenticated requests
401 Unauthorized on authenticated requests
403 Forbidden despite being logged in
403 Forbidden despite being logged in
Cause: Your user account lacks the required permission for this endpoint.Solution: Contact an administrator to grant the necessary permissions.
Credentials work in browser but not in API client
Credentials work in browser but not in API client
Cause: Some API clients don’t automatically handle cookies.Solution: Manually extract the
Set-Cookie header from the login response and include it in subsequent requests.Next Steps
Error Handling
Learn about error responses and HTTP status codes
