Skip to content
Next.js+Supabase Auth
Stack Integration

Next.js + Supabase Auth: Server-Side Pattern

Use cookie-based Supabase sessions with Next.js App Router. Verify claims in server code, refresh tokens in Proxy and enforce data access with Row Level Security.

Check current platform documentation ↗
Use Cases
  1. Server-rendered pages for signed-in users
  2. OAuth and password sign-in in the same app
  3. Session refresh across browser and server
  4. Per-user or per-tenant data protected by RLS
Implementation

Install @supabase/ssr and create separate browser and server clients. In Next.js 16, use proxy.ts for session refresh; in Next.js 15 and earlier, use middleware.ts. Use supabase.auth.getClaims() to verify identity in server code, getUser() when you need a fresh user record and RLS for table access. Check authorization again inside sensitive Route Handlers and Server Actions.

In detail

How the pieces connect

The @supabase/ssr package stores the session in cookies so Server Components and browser code can use the same sign-in state. A request passes through Next.js Proxy (middleware in Next.js 15 or earlier), which refreshes an expiring token and writes updated cookies to the request and response. Server code creates a request-scoped Supabase client and uses getClaims() to verify the access token before returning private data. The browser client handles interactive sign-in.

Production gotchas

Do not authorize a request with getSession() in server code: it reads the cookie without verifying identity. Use getClaims() for verified claims; use getUser() when you need the latest user record from the Auth server. If Proxy creates a new response after refreshing tokens, copy both the refreshed cookies and relevant cache headers to it or users may be signed out. Do not cache a response that contains another user's Set-Cookie header. Create a new server client for each request.

Security considerations

Authentication says who a user is; Row Level Security decides which rows they may access. Enable RLS on exposed tables and test policies with real user sessions. Keep service-role credentials server-only. Check authorization inside Route Handlers and Server Actions as well as at the page boundary; a navigation redirect alone does not protect a mutation. Validate post-login redirects against allowed destinations.

When this combo fits

This pairing fits when the product already uses Supabase for relational data and needs signed-in, server-rendered Next.js pages. It is less useful for a mostly static marketing site with one simple gated endpoint. For complex organization permissions, model membership explicitly and test the RLS policy before committing to the UI.

Other integration guidesView all →
Related

Need this built?

Email Dusko about a project

For a project enquiry, include what you’re building, its current stage and your target date.