Next.js + Supabase Auth
Use this skill for authentication work in Next.js App Router with Supabase Auth.
When to Apply
Use this skill when:
- Implementing login/signup/logout and session flows
- Protecting routes in
proxy.ts - Handling auth callback exchanges
- Wiring auth checks in Server Components, Server Actions, and route handlers
- Debugging cookie/session desync between server and client
Do not use this skill when:
- The task is primarily query/index/schema optimization (use
supabase-postgres-best-practices) - The task spans broader Supabase surface area beyond Next.js auth wiring (use
docs/ai/skills/supabase/SKILL.md) - The task is UI-only and does not change auth behavior
Core Rules
- Shared auth lives in
packages/auth(@asym/auth); readpackages/auth/AGENTS.mdfirst. Apps re-export package contracts instead of creating a second client. - Route protection and session refresh run in each app's
proxy.ts(Next.js 16 Proxy, formerly middleware) throughcreateAuthMiddlewarefrom@asym/auth/middleware, which validates the user withauth.getUser(). Public routes are listed explicitly there. - Server code resolves the caller with
getAuthContext,requireAuth, orrequireRolefrom@asym/auth/context. Business data access stays inpackages/api; app route handlers must not import@asym/database/supabase/*or@supabase/*directly (docs/guides/architecture/data-access-boundary.md). - Supabase clients come from
packages/database:@asym/database/supabase/serverfor server code,createBrowserClientfrom@asym/database/supabasefor the browser, and@asym/database/supabase/admin(service role) only in trusted server code. - Auth callbacks re-export the shared handler (
export { GET } from "@asym/api/auth/callback"). - Do not manually store or forward auth tokens in client code. Maintain RLS assumptions; do not bypass policy boundaries. Fail closed.
Workflow
- Identify the execution boundary first (Server Component, Client Component, proxy, Server Action, or route handler).
- Use the shared helper for that boundary; do not create a new Supabase client.
- Check the app's
proxy.tspublic-route list when adding or protecting a route. - Validate auth and role before sensitive reads or writes.
- Write the regression test first (
packages/auth/context.*.test.tscover the request auth context), then verify login, logout, and session refresh end to end.
Checklist
- [ ] Shared auth stays in
packages/auth; apps re-export - [ ] Protected and public routes are correct in
proxy.ts - [ ] Server code uses
@asym/auth/context; no direct Supabase imports in app route handlers - [ ] No service-role client outside trusted server code
- [ ] No manual token persistence logic added
Common Anti-Patterns
- Using the browser client in Server Actions or the server client in Client Components
- Trusting
getSession()in server render paths without validating the user - Storing JWTs manually in localStorage or cookies
- Adding an app-local Supabase client or auth callback instead of re-exporting the shared one