Documentation Index
Fetch the complete documentation index at: https://docs.getsabo.com/llms.txt
Use this file to discover all available pages before exploring further.
Use middleware to control request flow at the edge: session sync, redirects, headers/rewrites, or selective execution via matchers. This page centralizes patterns used across the app (not just the dashboard).
What middleware does
- Session sync: keep Supabase auth cookies in sync for SSR.
- Redirects: gate protected pages and bounce signed‑in users away from auth pages.
- Selective execution: use matchers to include/exclude paths for performance or correctness.
- Rewrites/headers: optionally set security headers or route rewrites (advanced).
Auth gate pattern (Supabase)
Key logic lives insabo/src/lib/supabase/middleware.ts:
Matcher design (include/exclude strategy)
- Start with the narrowest matcher that achieves your goal (e.g., only
^/dashboard), then expand if needed. - Exclude static assets, images, and favicons to reduce overhead.
- Avoid running middleware on webhook endpoints or any route requiring raw request bodies.
middleware.ts when you add one):
Top-level proxy file (used in this repo)
This repository ships with asrc/proxy.ts that delegates to updateSession() and declares a matcher. It is functionally equivalent to a top‑level middleware.ts.
Webhooks (Stripe or Polar)
Webhook handlers (e.g.,/api/webhooks/stripe or /api/webhooks/polar) verify signatures using the raw request body. Our default matcher in src/proxy.ts currently runs on all paths except static assets, so it will execute before these webhook routes. Because updateSession() never touches the request body, this works out of the box, but you can exclude the endpoints for extra safety:
src/proxy.ts
Roles / Tenant guards (skeleton)
You can extend the auth gate with role or organization checks:Prefer performing heavy data lookups in route handlers or layouts; keep middleware light. Cache claims if possible.
Security & performance checklist
- Correct scope: Match only the routes you need; exclude static and webhook paths.
- Early auth fetch: Call
auth.getUser()immediately after creating the client. - No heavy work: Avoid DB lookups or large network calls in middleware.
- Predictable redirects: Keep redirect targets simple and relative.
- Webhook integrity: Do not wrap webhook paths with auth middleware; perform signature verification inside the route.
See also
- Auth with Supabase (flows, UI, server actions) — route protection is summarized there and links back here for details.