Overview
Athena ERP integrates with Supabase for two primary functions:- Authentication - User authentication using Supabase Auth
- PostgreSQL Database - Managed PostgreSQL database hosting
Authentication
JWT Token Validation
Athena uses Supabase’s JWT-based authentication system. The backend validates tokens issued by Supabase Auth:- Validates JWT signature using shared secret
- Verifies token issuer matches configured Supabase URL
- Extracts user metadata (roles, school_id) from token
- Falls back to Supabase
/auth/v1/userendpoint if local validation fails
Configuration
Supabase authentication requires these environment variables: Backend (.env)Frontend Client
The React frontend initializes the Supabase client:Authentication Flow
- Login: User authenticates via Supabase Auth
- Token Issuance: Supabase returns JWT with user metadata
- Token Storage: Frontend stores token in Zustand store
- API Requests: Token sent in
Authorization: Bearerheader - Validation: Backend validates token and loads user context
- Authorization: Backend checks user permissions via
school_memberships
User Metadata
Supabase tokens include custom metadata inapp_metadata:
While tokens suggest user context, the backend always validates permissions against the local
school_memberships table.Multi-Tenant Authorization
Athena supports multi-school access via theX-School-Id header:
app/deps.py:get_auth_context for the complete authorization logic.
PostgreSQL Database
Connection
Athena connects to Supabase’s managed PostgreSQL using SQLAlchemy with async support:Database URL Format
Use the
asyncpg driver for async PostgreSQL connections with SQLAlchemy.Database Schema
Athena’s schema is defined inschema_mvp_ddl.sql and includes:
- Multi-tenant tables:
schools,school_settings,school_years - User management:
users,school_memberships - Academic structure:
grades,subjects,class_sections - Student records:
students,enrollments,grades - Communications:
messages,announcements
Connection Pooling
Dependency Injection
Database sessions are injected into route handlers:get_db dependency ensures:
- Automatic session lifecycle management
- Transaction commit on success
- Rollback on exceptions
- Proper session cleanup
Migration from Local to Supabase
Development Phase
During development, Athena can run with local PostgreSQL via Docker:Production Migration
- Create Supabase Project
- Copy JWT Secret from Supabase Dashboard → Settings → API
- Update Environment Variables with Supabase credentials
- Run Migrations against Supabase database
- Configure Row Level Security (optional)
- Update Connection Strings in Railway/deployment platform
Security Considerations
JWT Secret Management
Service Role Key
The service role key bypasses Row Level Security:- Store in backend environment only
- Use for admin operations and data migrations
- Never expose to frontend or client-side code
Connection Security
- Always use SSL for database connections
- Rotate database passwords periodically
- Restrict database access to application servers
- Use connection pooling to prevent resource exhaustion
Monitoring
Supabase Dashboard
Monitor database health via Supabase Dashboard:- Active connections
- Query performance
- Storage usage
- API usage