Overview
The Med Agenda API provides role-based authentication for three distinct user types:- Patients - End users scheduling and managing medical appointments
- Doctors - Medical professionals managing consultations and diagnoses
- Administrators - System administrators with elevated privileges
How Authentication Works
The authentication system uses email and password credentials with the following security measures:- Password Encryption: All passwords are hashed using BCrypt algorithm before storage
- Credential Validation: Email and password are verified against the database
- Response Handling: Returns success message or 401 error for invalid credentials
The current implementation uses basic authentication. For production environments, consider implementing JWT tokens or session-based authentication for enhanced security.
Authentication Flow
Patient Authentication
Login Endpoint
Endpoint:POST /patients/login
Source: PatientController.java:24-31
Authenticates a patient user and grants access to patient-specific functionality.
Request
Patient’s registered email address
Patient’s password (will be verified against BCrypt hash)
Example Request
Success Response
Status Code:200 OK
Error Response
Status Code:401 Unauthorized
Implementation Details
The patient authentication process:- Receives login request with email and password
- Calls
PatientService.authenticatePatient(email, password) - Service queries database for patient with matching email
- Verifies password using BCrypt password encoder
- Returns
Optional<Patient>- present if credentials valid, empty otherwise
Doctor Authentication
Login Endpoint
Endpoint:POST /doctor/login
Source: DoctorController.java:29-36
Authenticates a doctor user and grants access to medical professional functionality.
Request
Doctor’s registered email address
Doctor’s password (will be verified against BCrypt hash)
Example Request
Success Response
Status Code:200 OK
Error Response
Status Code:401 Unauthorized
Implementation Details
The doctor authentication process:- Receives login request with email and password
- Calls
DoctorService.authenticateDoctor(email, password) - Service queries database for doctor with matching email
- Verifies password using BCrypt password encoder
- Returns
Optional<Doctor>- present if credentials valid, empty otherwise
Administrator Authentication
Login Endpoint
Endpoint:POST /admin/login
Source: AdminController.java:23-30
Authenticates an administrator user and grants access to system administration functionality.
Request
Administrator’s registered email address
Administrator’s password (will be verified against BCrypt hash)
Example Request
Success Response
Status Code:200 OK
Error Response
Status Code:401 Unauthorized
Implementation Details
The admin authentication process:- Receives login request with email and password
- Calls
AdminService.authenticateAdmin(email, password) - Service queries database for admin with matching email
- Verifies password using BCrypt password encoder
- Returns
Optional<Admin>- present if credentials valid, empty otherwise
Session Management
The current implementation does not include session management or token generation. Each request is stateless.Recommended Enhancements
For production deployments, consider implementing:- JWT Tokens: Issue JSON Web Tokens upon successful authentication
- Session Storage: Store active sessions with expiration times
- Refresh Tokens: Implement token refresh mechanism for long-lived sessions
- Role-Based Access Control: Enforce permissions based on user roles
Example JWT Implementation (Recommended)
Security Considerations
Password Security
All passwords are encrypted using BCrypt hashing algorithm configured inSecurityConfig.java:27-29:
- One-way hashing (passwords cannot be decrypted)
- Salt generation for each password
- Adaptive hashing (computational cost increases over time)
HTTPS Requirement
CORS Configuration
Cross-Origin Resource Sharing is configured inWebConfig.java:12-18 to allow requests from:
http://localhost:5173(development frontend)https://final-project-poo2.vercel.app(production frontend)
GET, POST, PUT, DELETE, OPTIONS
CSRF Protection
CSRF protection is disabled (SecurityConfig.java:16) to support stateless API requests. This is acceptable for token-based authentication but should be reconsidered if implementing session-based authentication.
Error Handling
Common Authentication Errors
| Error | Status Code | Description |
|---|---|---|
| Invalid credentials | 401 Unauthorized | Email or password is incorrect |
| Missing fields | 400 Bad Request | Email or password not provided |
| Account not found | 401 Unauthorized | No user exists with provided email |
| Server error | 500 Internal Server Error | Database or service error |
Debugging Authentication Issues
- Verify email format: Ensure email is correctly formatted
- Check password: Passwords are case-sensitive
- Confirm user exists: Verify user was created successfully
- Database connectivity: Ensure database connection is active
- BCrypt configuration: Verify password encoder bean is properly configured
Testing Authentication
Using cURL
Test patient authentication:Using JavaScript/Fetch
Next Steps
After successful authentication, explore these related API endpoints:Patient Management
Create and manage patient accounts
Doctor Management
Create and manage doctor profiles
Admin Management
Manage administrator accounts
Consultations
Schedule and manage medical appointments