PatchPaste · Guides
Last updated 3 November 2026
Supabase select returns an empty array? Why auth.uid() is null
Scan your live app free after you fix it. It checks whether common Supabase tables can be read with your public anon key without a login, and searches the JavaScript your site sends for service_role and other secret keys. No login, no code access.
data: [] with no error usually means RLS is doing its job. A policy like using ((select auth.uid()) = user_id) only lets a row through when the request carries a signed-in user. With no user token, auth.uid() is null, null = user_id is never true, and every row is filtered out silently. Fix the missing session. Don't fix it by turning RLS off, writing using (true) or putting the service_role key in the browser: each of those makes the table readable by anyone.Why there's no error
Supabase describes a policy as an extra WHERE clause added to every query. Rows the USING clause rejects are simply not returned. Supabase's RLS guide lists the three ways a request is denied: a missing grant raises 42501, a failed WITH CHECK raises 42501, and a USING clause that filters the row out raises nothing and matches zero rows. So an error points at grants or inserts (see 42501 "permission denied" and "new row violates"), while an empty array points at the policy or the session.
Supabase's own docs warn that when there's no access token, or the session has expired, auth.uid() returns null, so a policy like auth.uid() = user_id silently fails.
Prove it in 2 minutes
This is the check from Supabase's troubleshooting guide. It tells you what the database thinks you are at the moment your query runs.
- In the Supabase SQL Editor, create a temporary function:
create function test_authorization_header() returns json language sql as $$ select auth.jwt(); $$;
- Call it in your code at the same place as the query that returns
[]:const { data, error } = await supabase.rpc('test_authorization_header') console.log('role:', data?.role, 'user id:', data?.sub, error) - Read the result:
- role is
anon: no user session reached the database. Go to causes 1 to 3 below. - role is
authenticated: the session is fine. Comparesubwith theuser_idvalues in the table, then look at cause 4. - role is
service_role: you're using an admin key. If this is browser code, stop and read service_role key exposed.
- role is
- Remove the function when you're done:
drop function test_authorization_header;
The same troubleshooting page also suggests switching RLS off, or a temporary using (true) policy, to see whether RLS is the cause. Only do that on a test project or a table with no real data. On a live app it opens the table to anyone with your anon key, which is already in your public JavaScript.
The five usual causes
1. The query runs before the session has loaded
In a React app (Lovable, Bolt), a component can fire its query on first render, before supabase-js has restored the signed-in session. That request goes out as anon and gets []. Wait for the session first. supabase-js's onAuthStateChange sends an INITIAL_SESSION event when it has loaded, then SIGNED_IN, SIGNED_OUT and so on:
useEffect(() => {
const { data } = supabase.auth.onAuthStateChange((event, session) => {
if (session) loadMyRows() // only query once a user is present
else clearMyRows()
})
return () => data.subscription.unsubscribe()
}, [])2. Server code doesn't have the user's session
In Next.js Server Components, Route Handlers or Server Actions, a plain createClient(url, key) knows nothing about the user in the browser, so every query runs as anon. Supabase's SSR guide says server rendering needs cookie-based session storage through @supabase/ssr: create the server client with createServerClient and the request's cookies, and on Next.js add the Proxy (called middleware.ts on Next.js 15 and earlier) that refreshes the token. Use supabase.auth.getClaims() to check who the user is on the server. The same guide says never to trust getSession() in server code, because it reads the cookie without re-validating it.
3. An Edge Function queries without the caller's token
A signed-in user calling your function with supabase.functions.invoke sends their session JWT in the Authorization header. Your function has to use it. Supabase's current pattern is withSupabase({ auth: 'user' }, ...) from @supabase/server, which gives you ctx.supabase scoped to that user's RLS policies. Keep verify_jwt on for user-facing functions. If you create the client yourself with only the anon key, the database sees anon.
The reverse trap: Supabase enforces RLS from the Authorization header, not the apikey. A client made with the service role key but with the user's token in Authorization, or an SSR client that picks up the user's cookies, runs as the user and gets RLS-filtered results. Keep admin work on a separate client.
4. The policy doesn't match your data
- Wrong column or empty column. If rows were inserted with
user_idleft blank, or the policy comparesauth.uid()withidinstead ofuser_id, nothing matches. Check a few rows against thesubvalue from the test above. - Wrong role. Supabase recommends naming the role with
to authenticatedso the policy never runs for anon. A policyto anononly won't apply to signed-in users. - Updates that "succeed" but change nothing. Supabase notes an
UPDATEneeds a matchingSELECTpolicy to work as expected. - Roles kept in user metadata. Don't base access on
user_metadatainauth.jwt(): signed-in users can change it themselves. Useapp_metadataor a roles table.
5. The session has expired
An expired access token counts as no token, so auth.uid() is null again. On a server-rendered Next.js app, Supabase's SSR guide makes refreshing the token the Proxy's job, so a missing Proxy (or one in a file Next.js never calls) shows up as users being signed out and data vanishing.
The safe policy, for reference
alter table public.todos enable row level security; create policy "Users read their own todos" on public.todos for select to authenticated using ( (select auth.uid()) = user_id );
Wrapping auth.uid() in (select ...) is Supabase's recommended form for speed, and an index on user_id helps too. If you want the intent spelled out, Supabase also shows auth.uid() IS NOT NULL AND auth.uid() = user_id. Need more patterns? See RLS using (true) and the Supabase RLS check.
The AI "fixes" to refuse
Ask Lovable, Bolt or Cursor to "make the data show up" and it may suggest one of these. All three make the empty array go away by making the table public:
alter table ... disable row level security: anyone with your anon key can read (and often write) every row.using (true)or a policyto anon: every visitor sees every row. Why using (true) is a trap →- Swapping in the service_role or
sb_secret_key in frontend code: it bypasses RLS completely, and it ships to every visitor. How to check and fix →
A prompt to paste into Lovable, Bolt or Cursor
My Supabase query returns an empty array for a signed-in user because auth.uid() is null in the RLS policy. Do NOT disable RLS, do NOT add a USING (true) policy or a policy for the anon role, and do NOT use the service_role or any secret key in frontend code. 1) Make sure user-specific queries only run after the Supabase session has loaded (use supabase.auth.onAuthStateChange and wait for a session). 2) If the query runs on the server or in an Edge Function, make it use the signed-in user's session (the @supabase/ssr server client with cookies, or the caller's Authorization header) and verify the user with getClaims(). 3) Check that the table's SELECT policy is "to authenticated using ((select auth.uid()) = user_id)" and that inserted rows set user_id to the signed-in user's ID. Tell me exactly which files and policies you changed.
Then test it yourself: signed out you should get nothing, as yourself you should get your rows, and as a second test user you should get only theirs.
Before launch, run the full Lovable/Bolt pre-launch checklist. Also see is my Lovable app secure?
Empty array and auth.uid() FAQ
Why does my Supabase select return an empty array with no error?
Row Level Security filters rows silently. A policy's USING clause acts like an extra WHERE on the query, so rows the caller isn't allowed to see are simply left out, with no error. If the policy compares auth.uid() to a user_id column and the request has no signed-in user, auth.uid() is null, null = user_id is never true, and you get [].
Why does auth.uid() return null in my RLS policy?
Because the request reached the database without a valid user access token. Supabase's docs say auth.uid() returns null when no access token is provided or the session has expired. Common reasons: the query ran before the session loaded in the browser, a server or Edge Function client was created without the user's token or cookies, or the user isn't actually signed in.
How do I check whether my Supabase request has a user session?
Supabase's troubleshooting guide suggests a tiny SQL function, select auth.jwt(), called with supabase.rpc() at the same place as the failing query. If the role it reports is anon (or service_role), the request has no user session. If it's authenticated, the sub value is the user ID your policy sees. Drop the function afterwards.
Should I turn off RLS or use USING (true) to fix the empty array?
No, not on a table with user data. Both make every row readable to anyone holding your public anon key, which ships in your site's JavaScript. If you want to prove RLS is the cause, use the auth.jwt() check instead, or test on a copy of the table with no real data.
Will using the service_role key fix it?
It hides the symptom and creates a worse problem. The service_role (or sb_secret_) key bypasses RLS entirely, so if it ends up in browser code anyone can read and change every table. Keep it in server-only code, and fix the missing session instead.
My Edge Function uses the service role key but still gets RLS errors. Why?
Supabase enforces RLS from the Authorization header, not the apikey. If you set Authorization to the user's token, or used an SSR client that pulls the user's session from cookies, requests run as that user even though the client was created with the service role key. Use one client per job: a user-scoped client for the caller's data and a separate admin client for admin work.
Can PatchPaste tell me why my query returns an empty array?
No. PatchPaste scans from outside, like a stranger, so it can't sign in as your users or read your policies. What it does check is the opposite risk: whether common tables answer to the public anon key without a login, and whether a service_role or sb_secret_ key is in your live JavaScript. That's worth running after you change policies.
Check your app for free
Paste your live URL for a free A to F report with every problem in plain English. Fix prompts for that report are a one-off £9 (or £29 for 10 reports). Scans are free to rerun, so you can check again after you change your policies and redeploy.
Want to see one first? View a sample report.
Sources (checked 3 Nov 2026): Supabase, Row Level Security (policies as an implicit WHERE; USING filtering raises nothing and matches zero rows; auth.uid() returns null without an access token or with an expired session; name the role with TO; (select auth.uid()) form; UPDATE needs a SELECT policy; user_metadata can be changed by users); Supabase troubleshooting, Why is my select returning an empty data array (the auth.jwt() RPC test); Supabase troubleshooting, Why is my service role key client getting RLS errors (RLS is enforced from the Authorization header); Supabase, Creating a Supabase client for SSR (cookie-based sessions, Proxy refresh, getClaims vs getSession); Supabase, Securing Edge Functions (withSupabase auth: 'user', verify_jwt); supabase-js, onAuthStateChange (INITIAL_SESSION and other events).