Supabase Cloud Favorites

EventMap now switches favorites by auth state: guests keep browser persistence, authenticated users use the Supabase favorites table.

Mode

Guest mode

A visitor without a Supabase session keeps using eventmap:favorites and eventmap:favorite-events in localStorage.

Mode

User mode

An authenticated user reads and writes public.favorites through the browser client and RLS-scoped policies.

Mode

Copy migration

On first sign in, cached local EventCardData snapshots are copied to Supabase. The local cache stays in place as guest fallback.

Hook boundary

useFavoritesCloud

The hook reads the current Supabase user in the browser after hydration. If there is no configured client or no user session, it delegates to the existing localStorage favorites hook.

guest localStorage → login → copy snapshots → cloud favorites

CRUD

SELECT

useFavoritesCloud loads event_id and event_snapshot from public.favorites. RLS limits the result to the current auth.uid().

CRUD

INSERT

Saving an event inserts user_id, event_id and event_snapshot. No upsert is used because this lesson has no UPDATE policy.

CRUD

DELETE

Removing a favorite deletes the matching user_id and event_id row, then the UI updates optimistically.

Boundary

Product page

/favorites stays a normal EventMap page. It shows events, empty state and a link back to search.

Boundary

Client island

FavoriteButton remains the small client island inside server-rendered event cards.

Boundary

Demo Lab

SQL, RLS and CRUD explanations stay here instead of leaking into product UI.