How cherryrambler/project--work works
1. Purpose and deployment shape
Area Authorization System is a single FastAPI backend plus React/Vite client for RBAC-managed geographic areas and survey records. The former auth/area split is consolidated into one API and one PostgreSQL/PostGIS database, avoiding cross-service user synchronization.
• Backend entry point: area-auth-api/app/main.py
• Frontend entry point: auth-frontend/src/main.jsx
• Client composition/state: auth-frontend/src/App.jsx
2. API composition and request boundary
app/main.py assembles versioned routers for authentication, authorized areas, and surveys. Routers are the HTTP boundary; Pydantic schemas validate request/response shapes before domain persistence.
• Authentication/session endpoints: area-auth-api/app/routers/auth.py
• Authorized-area and audit endpoints: area-auth-api/app/routers/areas.py
• Survey-record endpoints: area-auth-api/app/routers/surveys.py
• Contract definitions: area-auth-api/app/schemas/auth.py, area-auth-api/app/schemas/area.py, area-auth-api/app/schemas/survey.py
3. Identity, sessions, and authorization
The security path is: public registration/login validates credentials, persists a device/login session with refresh token, and returns JWTs. Protected requests decode access JWTs, reload the user from the database, reject deactivated users immediately, then apply self-or-admin or role checks. Login failures support lockout; admins can activate/deactivate and unlock accounts.
• JWT/password and token logic: area-auth-api/app/core/security.py
• Current-user and role dependencies: area-auth-api/app/core/dependencies.py
• User/session persistence: area-auth-api/app/models/user.py, area-auth-api/app/models/session.py
• Endpoint orchestration: area-auth-api/app/routers/auth.py
4. Geospatial authorization areas
Each user has at most one PostGIS polygon authorized area, keyed directly to the user and deleted on user deletion. Area mutations and reads enforce self-or-admin scope. Authenticated users can test whether a point falls within their own polygon. Shared geometry conversion/validation separates coordinate input from PostGIS model operations.
• Area routes and containment check: area-auth-api/app/routers/areas.py
• PostGIS area model: area-auth-api/app/models/authorized_area.py
• Geometry utilities: area-auth-api/app/core/geo_utils.py
5. Survey-record lifecycle
Survey records are distinct from authorized areas: users can own many polygon-backed, village/plot-labeled records. Admins create, list, update, reassign, and delete them; record access and verification are self-or-admin according to the endpoint rules. Survey mutations emit audit events.
• Lifecycle endpoints: area-auth-api/app/routers/surveys.py
• Persistent record model: area-auth-api/app/models/survey_record.py
• Input/output validation: area-auth-api/app/schemas/survey.py
6. Persistence, audit, and infrastructure
SQLAlchemy async database access targets PostgreSQL 14+ with PostGIS. The primary relational graph is users → sessions, authorized area, surveys, and audit events; session, area, and survey foreign keys cascade on user deletion. Audit logs deliberately retain a nullable non-FK user ID and capture action, resource, success, and request IP.
• DB engine/session and declarative base: area-auth-api/app/db/session.py
• Audit model/logger/actions: area-auth-api/app/models/audit_log.py, area-auth-api/app/core/audit_logger.py, area-auth-api/app/core/audit_actions.py
• Environment configuration: area-auth-api/app/core/config.py
7. Frontend/API flow
The React client manages active auth state directly in App.jsx. API calls use a configured backend URL and localStorage tokens; a shared request layer coalesces refresh attempts on 401, retries the failed request with a new access token, and signals logout only when refresh fails. Dashboard UI displays and manages the caller’s area without external map tiles.
• Shared fetch, refresh, and API base handling: auth-frontend/src/api/config.js
• Auth/area request calls: auth-frontend/src/api/auth.js
• Login/dashboard views: auth-frontend/src/pages/AuthPage.jsx, auth-frontend/src/pages/DashboardPage.jsx
Main entry points: areas.py Area endpoints · session.py Async DB access · auth.py Auth endpoints · surveys.py Survey endpoints
Components 18
FastAPI backend
- API assembly · Mounts versioned auth, area, and survey routers.
area-auth-api/app/main.py - Auth endpoints
area-auth-api/app/routers/auth.py - Area endpoints · Manages authorized areas, containment checks, and audit-facing actions.
area-auth-api/app/routers/areas.py - Survey endpoints
area-auth-api/app/routers/surveys.py - API contracts · Validates authentication, area, and survey payloads.
area-auth-api/app/schemas/auth.py - JWT and passwords · Issues tokens and validates credentials.
area-auth-api/app/core/security.py - Current-user guards · Reloads users and enforces role or self-or-admin scope.
area-auth-api/app/core/dependencies.py - Geometry validation · Converts coordinate input for PostGIS operations.
area-auth-api/app/core/geo_utils.py - Audit logger · Records mutation outcome, resource, action, and request IP.
area-auth-api/app/core/audit_logger.py - Async DB access
area-auth-api/app/db/session.py
React client
- Client composition · Owns active authentication state and view composition.
auth-frontend/src/App.jsx - API request layer · Uses local tokens, coalesces refreshes, and retries 401s.
auth-frontend/src/api/config.js - Login view
auth-frontend/src/pages/AuthPage.jsx - Area dashboard · Displays and manages the caller's area.
auth-frontend/src/pages/DashboardPage.jsx
PostgreSQL / PostGIS
- Users and sessions · Device/login sessions retain refresh tokens and cascade with users.
area-auth-api/app/models/user.py - Authorized areas · One user-owned polygon per user.
area-auth-api/app/models/authorized_area.py - Survey records · Many polygon-backed village or plot records per owner.
area-auth-api/app/models/survey_record.py - Audit logs · Retains nullable actor IDs without a user foreign key.
area-auth-api/app/models/audit_log.py
Connections 26
- Client composition → Login view: renders
- Client composition → Area dashboard: renders
- Client composition → API request layer: uses
- API request layer → API assembly: HTTPS with JWT
- API assembly → Auth endpoints: mounts
- API assembly → Area endpoints: mounts
- API assembly → Survey endpoints: mounts
- Auth endpoints → API contracts: validates
- Area endpoints → API contracts: validates
- Survey endpoints → API contracts: validates
- Auth endpoints → JWT and passwords: login and refresh
- Auth endpoints → Current-user guards: protects requests
- Area endpoints → Current-user guards: enforces scope
- Survey endpoints → Current-user guards: enforces scope
- Area endpoints → Geometry validation: converts polygons
- Auth endpoints → Async DB access: persists identity
- Area endpoints → Async DB access: reads and writes
- Survey endpoints → Async DB access: reads and writes
- Async DB access → Users and sessions: maps
- Async DB access → Authorized areas: maps
- Async DB access → Survey records: maps
- Audit logger → Audit logs: writes
- Area endpoints → Audit logger: logs mutations
- Survey endpoints → Audit logger: logs mutations
- Users and sessions → Authorized areas: one user area; cascade delete
- Users and sessions → Survey records: owns many; cascade delete