◉RORK LABJP
●GPT-6.1 SOL — Added to Rork's model menu (09-29) with Low, Medium and High effort; included on Pro and Max●SONNET 5.5 — Claude Sonnet 5.5 is selectable: strong at UI design and faster than Sonnet 5 (09-28)●SDK 58 — Beta since 09/15 with no stable date yet; running projects in Expo Go has required login since 09/03●WIDGET — A write-up traces an iOS widget that renders black only in production builds●OBSERVE — EAS Observe now records native crashes too (SDK 57.0.21 or later, 10/07)●NEW — My App Open Ad Only Showed When the App…●GPT-6.1 SOL — Added to Rork's model menu (09-29) with Low, Medium and High effort; included on Pro and Max●SONNET 5.5 — Claude Sonnet 5.5 is selectable: strong at UI design and faster than Sonnet 5 (09-28)●SDK 58 — Beta since 09/15 with no stable date yet; running projects in Expo Go has required login since 09/03●WIDGET — A write-up traces an iOS widget that renders black only in production builds●OBSERVE — EAS Observe now records native crashes too (SDK 57.0.21 or later, 10/07)●NEW — My App Open Ad Only Showed When the App…
Articles/App Dev
◇ App Dev/2026-06-24Intermediate

Three Implementations You Always Face with Next.js and Supabase — RLS, State, and Real-Time

Past building the foundation of a web app lie three things you always get stuck on — DB access control (RLS), screen state management, and real-time updates — explained at the implementation level with Next.js and Supabase code. Down to production pitfalls like memory leaks from forgotten unsubscribes and how auth ties into RLS.

Next.jsSupabase37RLS3state management5real-time4authpremium4

✦ Premium Article

The previous article drew the big picture of a web app and the move of leaving the foundation to a BaaS. This follow-up steps into the three implementations you will inevitably face on that foundation: DB access control, screen state management, and real-time updates. Each is the classic "I got something working, but I stall before shipping."

As an indie developer building app backends, I got stuck on each of these three in a different way. Let me sort them out in order, with code.

Why "anyone can hit the DB directly" is dangerous

The flip side of a BaaS's convenience is that the browser can call the DB directly. That leads to the natural worry, "could a malicious person see other people's data too?" That worry is correct, and what answers it is RLS (Row Level Security).

RLS is the feature of "setting, on each row of a table, who can access it, in the DB itself." The query is the same whoever sends it, but RLS looks at the sender and automatically narrows the rows it returns.

-- enforce "you only see tasks you created" at the DB level
alter table tasks enable row level security;
 
create policy "own tasks only"
  on tasks for select
  using (auth.uid() = owner_id);

In ordinary programming you tend to think "access control is written in app code (if statements)," but in the BaaS world the DB itself holds the rule. So even if there is a bug in the frontend code, leakage of other people's data is prevented at the DB level. My recommendation is to always make "create a table, enable RLS, write the policy" one single unit of work. Put it off, and it ships to production as is and becomes an incident.

Auth and RLS are linked through the same token

The auth.uid() that appeared in the RLS policy is the very token representing login state. Hand-rolling login is hard, but with a BaaS you handle it like a library.

// sign in
const { data, error } = await supabase.auth.signInWithPassword({
  email: 'user@example.com',
  password: 'password123',
});
 
// get the currently signed-in user
const { data: { user } } = await supabase.auth.getUser();

What helps to know is that login state is managed by a token stored in the browser. The browser holds the token received at login and sends it along on later requests, telling the server "this is that signed-in user." That token is the contents of the RLS auth.uid(). So auth and RLS are linked through the same token. Once that connects in your mind, the whole picture of access control clicks at once.

✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦How to write RLS policies that protect other users' data when the browser hits the DB directly, and how they tie to the auth token
✦A minimal global state management (Zustand) implementation that avoids prop drilling, plus when to use it
✦Concrete useEffect cleanup code that prevents memory leaks from forgotten Realtime unsubscribes
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Rork Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

◇ App Dev2026-09-29
Your Expo Router App Flashes the Sign-In Screen on Every Launch. Rebuild the Guard With a Third State: Not Known Yet
Signed in, yet the sign-in screen flashes at launch; signed out, yet the screen never changes. How I rebuilt Expo Router Protected routes around three Supabase auth states, including the one the docs never name, and where the _layout.tsx files have to live.
◇ App Dev2026-10-15
My Ads Never Came Back After a Subscription Expired: Fixing the Direction of an Ad-Free Check
An ad-free flag stayed on after the subscription expired because the check pointed the wrong way. Here is how I rebuilt it to wait for two purchase queries, decide only from confirmed facts, and keep the last value on failure.
◇ App Dev2026-10-15
When adjustsFontSizeToFit Shrinks Just One Row, Suspect the Box Height, Not the String
Only the 10th row of a list drops to 4pt on iOS, though the data and strings are fine. I read the Yoga rounding and font-fit sources, then counted how often the box comes up short in Node.