NextBase Auth SSR
Load the vendored supabase skill as well. Verify current Supabase SSR guidance before changing auth behavior.
Existing Client Boundaries
- Browser code uses
createClient()fromsrc/supabase-clients/client.ts. - Server Components, actions, and route handlers use
createSupabaseClient()fromsrc/supabase-clients/server.ts. src/supabase-clients/middleware.tsowns request/response cookie refresh. Preserve the completegetAll/setAllpropagation to both the request and response.src/proxy.tsowns route matching and delegates session refresh. Keep API and static-asset exclusions deliberate.
Only the publishable key may be used in browser-visible code. Never expose a secret or service_role key.
Verification Choice
- Use
getUser()when the request boundary must revalidate the user with Supabase, especially middleware/proxy route protection and sensitive re-verification. - Use
getClaims()for locally verified claims and authenticated IDs inside already protected server work when its guarantees are sufficient. - Do not authorize from
getSession()alone. A locally stored session is not a fresh authorization check. - Use the request-memoized helpers in
src/rsc-data/supabase.tsinstead of repeating auth calls during one render.
Middleware and Protected Routes
- Do not insert unrelated work between
createServerClient()and the auth verification call in middleware. - Match actual public URL segments, including nested routes. Test both the route root and a representative child route.
- Redirect anonymous users to
/loginwhile preserving cookies set on the Supabase response. - Do not treat the protected-route list as data authorization; RLS remains mandatory.
Callback Safety
- Exchange the code or verify the OTP before redirecting.
- Accept a
nextdestination only when it is a same-origin relative path beginning with one/and not//. Reject encoded or decoded absolute URLs and protocol-relative URLs. - Construct the final destination from an approved path and the request origin. Never pass arbitrary user input directly to
new URL(). - Send failed verification to
/auth/auth-code-errorand avoid logging tokens or cookie values. - Revalidate only after auth state changes successfully.
Verification
Cover anonymous redirect, authenticated access, cookie refresh, callback success, callback failure, and malicious next values. Use the real local Supabase stack for auth integration tests.