◉RORK LABJP
●SUBDOMAIN — Rork can now connect your web app to a subdomain only, leaving your root domain and www untouched (12/1)●UPDATES — expo-updates 58.0.23 fixes Android release builds embedding a stale runtime version●FLASH END — Intro pricing for Gemini Flash models ends 12/31, 29 days from now. Time to recheck AI feature costs●HMR — Some report the dev server picking up another server's bundle and then exiting●FIRST VIEW — Map the first screen for a normal launch, a notification, and a link, and back navigation stops surprising you●CARBON — Gemini 4 Carbon and Argon are now at standard rates: $4.00 input, $20.00 output per 1M tokens●SUBDOMAIN — Rork can now connect your web app to a subdomain only, leaving your root domain and www untouched (12/1)●UPDATES — expo-updates 58.0.23 fixes Android release builds embedding a stale runtime version●FLASH END — Intro pricing for Gemini Flash models ends 12/31, 29 days from now. Time to recheck AI feature costs●HMR — Some report the dev server picking up another server's bundle and then exiting●FIRST VIEW — Map the first screen for a normal launch, a notification, and a link, and back navigation stops surprising you●CARBON — Gemini 4 Carbon and Argon are now at standard rates: $4.00 input, $20.00 output per 1M tokens
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.js3Supabase41RLS3state 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-11-24
Why Only the First Page of Your Carousel Is Blank at Launch, and How to Make FlatList Start on the Right Page
My wallpaper app opened on an empty first page. Swiping showed page two just fine, and swiping back left page one empty again. The pager had been told where to start before it knew its own size. Here is the Android fix and the Expo FlatList pattern that closes the same gap.
◇ App Dev2026-11-16
I Set cameraPermission to false for a Photo Picker, and the QR Scanner Lost Its Camera Too
In expo-image-picker, cameraPermission: false deletes NSCameraUsageDescription from Info.plist on iOS and blocks CAMERA for every package on Android. Here is what happens when you also use expo-camera, and a working script that checks permissions before you submit.