Skip to content

auto-bridge: read the Supabase session from cookies (Next.js / @supabase/ssr) #8

Description

@DorianOuvrard

Problem

The Supabase identity auto-bridge (startSupabaseIdentityBridge) only reads the session from localStorage (sb-*-auth-token / supabase.auth.token). That covers SPA-style clients (Lovable/Bolt/Vite on the default @supabase/supabase-js storage), but the default Next.js + @supabase/ssr setup keeps the session in a cookie so the server can read it for SSR/middleware. For that whole class of apps the auto-bridge finds nothing and replay stays anonymous, even though the session is sitting in a JS-readable cookie.

Evidence (a default Next.js + @supabase/ssr app)

  • cookie: sb-<ref>-auth-token = "base64-<base64 json>" — readable via document.cookie, not httpOnly
  • localStorage: no auth keys

The session is present and readable, just in a place the bridge does not look.

Desired behavior

With no builder code, a Next.js / @supabase/ssr app's signed-in user is picked up by the auto-bridge and attached to replay (source supabase-auto), the same way localStorage apps already work today.

Sketch

Extend the auto reader to also scan document.cookie for sb-*-auth-token (and reassemble chunked .0/.1 cookies in order), URL-decode, strip the base64- prefix, base64-decode, then feed the result through the existing _sessionFromParsedValue / _identityFromSession path. Keep the 2s poll (cookies emit no change event).

Acceptance criteria

  • A Next.js / @supabase/ssr app with the session only in cookies gets identity set automatically, with no identify() / connectSupabase() call
  • Chunked auth cookies (sb-*-auth-token.0 / .1) are reassembled correctly
  • localStorage-based apps keep working (no regression)
  • httpOnly auth cookies are a no-op for this path (documented: server-seeded identity is the path there)
  • No identity is set when no readable session exists

Out of scope

  • Server-seeded identity for httpOnly-cookie setups (separate: needs an init-time marker the SDK reads)
  • The sign-out / clear-identity gap (tracked in vulk-corp/bworlds#918)

Context

Default session storage differs by stack: @supabase/supabase-js -> localStorage; @supabase/ssr (Next.js / SvelteKit / Remix) -> cookies. connectSupabase(client) and identify() already work for cookie apps; this issue is about the zero-config auto path. Relates to vulk-corp/bworlds#918 (sign-out identity) and the BWorlds dogfooding work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions