π§ Optimal Division of Responsibilities: Next.js + FastAPI + Supabase (Hybrid Architecture)
This hybrid architecture works great across web, mobile, and desktop.
Let Supabase handle fast, low-risk interactions, and let FastAPI handle complex or sensitive operations.
π Division of Responsibilities
Type of Operation | Handled By | Why / Notes |
---|---|---|
User Auth (sign up, login, logout) | β Supabase SDK | Supabase Auth handles JWTs, sessions, password reset, etc. |
Session / JWT Management | β Supabase SDK | Handles refresh tokens and session persistence |
Fetching public or user-owned data | β Supabase SDK (with RLS) | Fastest path; use Row-Level Security for access control |
Realtime subscriptions (live updates) | β Supabase Realtime | WebSocket push from Supabase; fast and scalable |
Inserts/updates/deletes with business logic | β FastAPI | Central place for validation, permission checks, logging |
Admin or privileged actions | β FastAPI | Never expose to frontend directly |
Complex queries or joins | β FastAPI | Abstract DB schema from frontend; Postgres views or raw SQL |
File uploads (images, etc.) | β Supabase Storage OR FastAPI | Use Supabase for user uploads; FastAPI for secure preprocessing |
Analytics / logs / internal metrics | β FastAPI | Easier to centralize and process |
Multi-table transactions | β FastAPI | Wrap in DB transactions |
Custom dashboards, metrics, aggregates | β FastAPI | Compose and cache efficiently server-side |
Device discovery, IoT interactions | β FastAPI | Best handled by backend logic and devices |
π§± Folder/Service Structure Suggestion
β FastAPI (Backend)
Handles:
- /api/users/me
β returns enriched user profile
- /api/tasks/
β task CRUD with validation
- /api/admin/...
β admin-only endpoints
- /api/devices/
β device management, discovery, etc.
π Verifies Supabase JWTs via:
- Public JWKs: https://<your-project>.supabase.co/auth/v1/keys
- Adds user context to request via dependency injection
β Next.js (Frontend)
Uses:
- @supabase/auth-helpers/nextjs
for auth/session management
- @supabase/supabase-js
for:
- Realtime subscriptions
- Authenticated reads with RLS
- Simple inserts/updates
Calls FastAPI for: - Anything requiring cross-table logic - Custom APIs or secured logic
β Supabase (Postgres + Auth + Storage)
Enables: - Row-Level Security (RLS) to enforce per-user access - Postgres Functions (RPC) for efficient direct calls - Realtime for live updates - Storage for user-uploaded media
π Supabase RLS Example
CREATE POLICY "Users can access their own tasks"
ON tasks
FOR ALL
USING (user_id = auth.uid());
This allows safe read/write access to tasks from the frontend.
π Decision Tree (Frontend Developer Perspective)
π§ Ask yourself:
-
Is this data public or user-specific?
β Use Supabase directly if RLS can safely control access. -
Does this write action need validation, side effects, or audit logging?
β Go through FastAPI. -
Is this action admin-level or involves other usersβ data?
β Go through FastAPI. -
Does this require aggregating multiple tables or joining with business logic?
β Go through FastAPI. -
Is this realtime or chat/live dashboard?
β Use Supabase Realtime directly.
π Example Workflows
β€ User Views Their Task List
Frontend β Supabase (RLS):
select * from tasks where user_id = auth.uid()
β€ User Creates a New Task
Frontend β FastAPI /api/tasks/
FastAPI does the following: - Verifies JWT - Validates fields - Adds created_at, owner_id - Inserts into Supabase
β€ User Watches for Realtime Task Updates
- Frontend subscribes to Supabase Realtime on
tasks
table (filtered byuser_id
)
β Summary This hybrid setup gives you:
- The speed of Supabase
- The control of FastAPI
- Clean scalability for web, mobile, and desktop