Authentication Implementation Summary
What Was Implemented
A complete JWT-based authentication system for the DermaDetect API Gateway.
Key Features
β JWT Authentication
- Access tokens (30 min expiry)
- Refresh tokens (30 days expiry) with rotation
- HS256 algorithm with configurable secret key
β User Management
- User registration and login
- Role-based access control (patient, physician, admin)
- Multi-tenancy support via tenants table
- Physician-specific profiles
β Password Management
- bcrypt password hashing
- Password reset flow with secure tokens
- Password change for authenticated users
β Database Schema
users- Main authentication tablephysicians- Physician profilestenants- Multi-tenancyrefresh_tokens- Token storagepassword_reset_tokens- Reset tokens
β
API Endpoints (/api/auth)
- POST
/register- Create account - POST
/login- Authenticate - POST
/refresh- Refresh tokens - GET
/me- Get current user - POST
/request-reset- Request password reset - POST
/reset-password- Reset password - POST
/change-password- Change password
β Migration Tools
- Database migration for auth tables
- Physician migration script from
dd_api.groups(44 physicians) - Auto-generates password reset tokens
β Documentation
- Comprehensive API documentation
- Client integration examples
- Security best practices
Quick Start
1. Run Migrations
cd services/api_gateway
uv sync
uv run alembic upgrade head2. Migrate Existing Physicians (Optional)
uv run python -m scripts.migrate_physiciansThis will output password reset URLs for all 44 physicians.
3. Start the Server
uv run uvicorn src.main:app --reload --port 80004. Test the API
# Register a new user
curl -X POST http://localhost:8000/api/auth/register \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"password123","role":"patient"}'
# Login
curl -X POST http://localhost:8000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"password123"}'
# Get current user (replace TOKEN with access_token from login)
curl http://localhost:8000/api/auth/me \
-H "Authorization: Bearer TOKEN"Dependencies Added
"python-jose[cryptography]>=3.3.0" # JWT
"passlib[bcrypt]>=1.7.4" # Password hashing
"email-validator>=2.0.0" # Email validationFile Structure
services/api_gateway/
βββ src/
β βββ api/
β β βββ auth.py # Auth endpoints
β β βββ auth_test.py # Auth tests
β βββ core/
β β βββ auth.py # JWT & password utilities
β β βββ dependencies.py # Auth dependencies
β βββ schemas/
β β βββ auth.py # Pydantic schemas
β βββ database/
β βββ models.py # Updated with auth models
βββ scripts/
β βββ migrate_physicians.py # Migration script
βββ alembic/versions/
βββ f788c6af4f90_*.py # Auth tables migrationConfiguration
Set in .env or environment variables:
SECRET_KEY=your-secret-key-here # Auto-generated if not set
DATABASE_URL=postgresql://... # Default: localhostProtected Routes Example
from typing import Annotated
from fastapi import Depends
from core.dependencies import get_current_user, get_current_physician
from database.models import User
@router.get("/protected")
def protected_route(
current_user: Annotated[User, Depends(get_current_user)]
):
return {"user_id": str(current_user.uuid)}
@router.get("/physician-only")
def physician_route(
current_user: Annotated[User, Depends(get_current_physician)]
):
return {"physician_id": str(current_user.physician.uuid)}Client Integration (React Native / Web)
// Register
const { access_token, refresh_token } = await fetch('/api/auth/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: 'user@example.com',
password: 'password123',
role: 'patient'
})
}).then(r => r.json());
// Use access token
const response = await fetch('/api/protected', {
headers: { 'Authorization': `Bearer ${access_token}` }
});
// Refresh when needed
const newTokens = await fetch('/api/auth/refresh', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ refresh_token })
}).then(r => r.json());Next Steps
- β Basic auth system implemented
- π² Email service integration - For password resets
- π² Email verification - Verify user emails
- π² Rate limiting - Prevent brute force attacks
- π² 2FA/MFA - Additional security layer
- π² OAuth providers - Google, Apple Sign-In
- π² SAML/SSO - Enterprise authentication
Migration from Legacy System
The USER_MIGRATION_ANALYSIS.md document contains detailed analysis of the legacy data:
- Physicians: 44 users migrated from
dd_api.groups - Patients: Cannot migrate (only Firebase UIDs, no email/password)
- Tenants: 19 unique vendors/tenants created
- Strategy: Physicians require password reset, patients remain as anonymous legacy references
Documentation
Full documentation available at:
/docs/app/services/api-gateway/authentication/page.mdx- API docs:
http://localhost:8000/docs(when server is running)
Last updated on