DocsAuthentication

Auth flow, middleware, and role-based access

Authentication

Surflink uses Supabase Auth for user authentication, with server-side session management via the @supabase/ssr package.

Auth Flow

  1. Sign Up / Sign In -- Users authenticate via email/password or OAuth through the /login and /signup pages
  2. Session Management -- Supabase stores the session in HTTP cookies, managed by the SSR middleware
  3. Token Refresh -- The middleware automatically refreshes the auth token on every request
  4. Profile Creation -- On first OAuth callback, a coaches profile row is auto-created for the authenticated user

Middleware

The auth middleware (src/middleware.js) runs on every request except static assets and the video stream endpoint. It performs these tasks:

  1. Creates a Supabase SSR client with cookie-based session management
  2. Refreshes the auth token via supabase.auth.getUser()
  3. Cleans up stale chunked cookies -- Supabase SSR creates chunked cookies (sb-xxx-auth-token.0, .1, .2, etc.) that can accumulate and cause 431 (Header Too Large) errors
  4. Enforces route protection -- Unauthenticated users are redirected to /login on protected routes
  5. Redirects authenticated users away from /login and /signup to /dashboard

Public Routes

These routes are accessible without authentication:

RoutePurpose
/Landing page
/loginSign in
/signupCreate account
/auth/callbackOAuth callback
/reports/*Shareable session reports
/invite/*Student invite acceptance
/highlights/*Public highlight reels
/docs/*Documentation
/api/auth/frameio/*Frame.io OAuth flow

Supabase Clients

The project maintains three Supabase client configurations:

FileUsage
src/lib/supabase-browser.jsClient-side queries from React components
src/lib/supabase-server.jsServer-side queries in API routes
src/lib/supabase.jsLegacy client (maintained for compatibility)

Role-Based Routing

RoleRoute PrefixDescription
Coach/dashboard, /sessions, /students, etc.Full platform access
Student/student/*Read-only access to own data
Parent/parent/*View-only access to linked student

The AuthProvider context exposes the current user, coach profile, and loading state. Components use the useAuth() hook to access this context.

OAuth Callback

The OAuth callback handler at /auth/callback exchanges the authorization code for a session, then checks if a coaches row exists for the authenticated user. If not, it creates one automatically with the user's email and metadata.