TrademarkTrademark

Auth

Sign-in on Supabase or Better Auth, profiles and route gating, with a demo mode that needs no keys.

The auth slice is sign-in: email and password, a profile per user, route gating on every request, and an Upstash rate limiter in front of it. A composition that never needs an account (docs, store, landing) ships none of this code and none of its dependencies.

It runs on one of two providers, each a slice of its own:

  • supabase, the default: Supabase Auth, the app's tables in Supabase's Postgres, avatars in Supabase Storage.
  • better-auth: Better Auth with sign-in and the app's tables in one Postgres you connect to directly, such as Neon's free plan. No Supabase project needed.

The rest of the app imports sign-in through src/lib/auth/ and never a provider directly, so organizations, billing and the API surface work the same on both.

You rarely name it yourself. dashboard requires orgs, and orgs requires auth, so the scaffolder pulls it in transitively.

What it ships

  • src/lib/auth/ — the provider-neutral facade: isAuthConfigured(), getSessionUser(), requireUserAndService(), sign in and out in the browser, the proxy step, safeNextPath.
  • src/lib/sb/ or src/lib/better-auth/ — the provider.
  • src/components/UserProvider.tsx — useUser(): profile, email, global role, refresh.
  • src/app/(auth)/ — /auth (log in, sign up, forgot password) and /auth/update-password.
  • src/app/api/profile/ — GET and PATCH the profile, POST /api/profile/avatar.
  • src/proxy.ts — rate limiting, the session refresh and route gating.
  • supabase/init.sql or neon/auth.sql — profiles, roles, user_global_roles and the sign-up trigger (plus the profiles bucket on Supabase, and Better Auth's own tables on Postgres).

It adds @upstash/ratelimit and @upstash/redis, plus the provider's: @supabase/ssr and @supabase/supabase-js, or better-auth and pg.

Environment

The older NEXT_PUBLIC_SUPABASE_ANON_KEY and SUPABASE_SERVICE_ROLE_KEY names are still read, so an existing .env keeps working. Run supabase/init.sql in the project's SQL editor first; it is idempotent.

On Better Auth:

Run neon/auth.sql on that database first. Confirmation and password reset emails go through the email slice; without it, confirmation is off and avatar uploads wait for a storage slice.

Demo mode

Every variable is optional. Without the provider's keys (the Supabase URL and publishable key, or DATABASE_URL and BETTER_AUTH_SECRET), isAuthConfigured() is false and the app runs in demo mode: nothing is gated, /auth explains that authentication is disabled and offers the demo, UserProvider starts settled with no user, and the profile routes answer 503 not_configured instead of throwing. Without the two Upstash variables there is no proxy rate limiting (the chat surface's Claude route is the exception: in production it refuses to answer without them). A composition has to build and run with zero environment, so every cloud code path goes through these checks.

Route gating

src/proxy.ts runs on everything except static assets. It rate limits first, then checks the session (on Supabase, updateSession in src/lib/sb/middleware.ts also refreshes the cookie) and redirects a signed-out visitor away from the protected prefixes, /app and /profile:

The forms send the user back to next after signing in, through safeNextPath, which only honours a plain same-site path so the sign-in page cannot become an open redirect. With no next, sign-in lands on /app, or on / in a composition without the dashboard.

The proxy answers one question: is there a session. Whether that session belongs in a particular organization needs the database, and is the job of the organization guards.

The rate limit is a sliding window of 30 requests a minute per IP and path. A refusal is a 429 in the same { error: { code, message } } shape the route handlers use, with retry-after set.

Profiles and roles

The sign-up trigger creates a profiles row and a member global role for every sign-up. API routes act through requireUserAndService(), which resolves the signed-in user and returns the service client (Supabase's service-role client, or on Better Auth the same query builder run as plain SQL): nothing in the database scopes rows to a user, so ownership is enforced in the route by filtering on the user's id. The global role (admin, moderator, member…) is about the person across the whole app, not about any team.

Password reset

"Forgot password" emails a link to /auth/update-password. On Supabase, add that URL to the redirect URLs in the dashboard; the email signs the user in with a recovery session, so the page only sets the new password. On Better Auth the link carries a one-time token the page sends with the new password.

Shared Supabase projects

The supabase provider assumes the Supabase project belongs to this app alone. An app joining a project other products already use must not run init.sql as it is:

  • Prefix every table, bucket and function with the app's name.
  • Skip the trigger on auth.users, which would fire for every product's sign-ups, and create the per-user row lazily from the route that first needs it.
  • Drop the role tables unless the product has a real admin surface.

The scaffolder does not rewrite SQL; it prints a reminder whenever the slice is included.

Last updated November 13, 2026
Edit this page on GitHub