Supabase auth.uid() returns null – where it works, where it can't, and how to test policies

By Abheesht Tripathi · Published

select auth.uid();  →  NULL   (and RLS policies using auth.uid() match no rows)

What auth.uid() actually reads

auth.uid() does not look at a login session in the database. It reads the sub claim of the JWT that the Supabase API placed into the transaction settings (request.jwt.claims) for the current request. You can see the exact definition with:

select pg_get_functiondef('auth.uid'::regproc);

No JWT on the request means no sub, which means null. And null = user_id is never true, so policies built on it match nothing.

Where it is null

Test a policy as a real user in SQL

Switch to the authenticated role and set the claims yourself, inside a transaction that you roll back:

begin;
set local role authenticated;
select set_config('request.jwt.claims',
  json_build_object('sub', '<a-real-user-uuid>', 'role', 'authenticated')::text,
  true);

select auth.uid();              -- now returns that uuid
select * from public.notes;     -- only the rows this user may see

rollback;

The third argument true keeps the setting local to the transaction. The SQL Editor also has a role selector that can run a query as a chosen user, which does the same thing for you.

Pass the user's token from server code

In an Edge Function or API route, forward the caller's Authorization header so the database sees the same user:

import { createClient } from 'npm:@supabase/supabase-js@2';

Deno.serve(async (req) => {
  const supabase = createClient(
    Deno.env.get('SUPABASE_URL')!,
    Deno.env.get('SUPABASE_ANON_KEY')!,
    { global: { headers: { Authorization: req.headers.get('Authorization') ?? '' } } },
  );
  const { data, error } = await supabase.from('notes').select('*'); // runs as the caller, under RLS
  return Response.json({ data, error });
});

In Next.js, use the cookie-based server client from @supabase/ssr, which does this from the session cookie.

Jobs that run without a user

Background jobs that use the secret key must set ownership explicitly (insert ... (user_id, ...) values ('<uuid>', ...)) instead of relying on default auth.uid(), and must filter by user themselves, because RLS no longer does it for them.

Triggers see whoever caused the write

Because the claims live in the transaction, a trigger fired by an API request sees the requesting user in auth.uid(). The same trigger fired by a SQL Editor statement, a migration or a cron job sees null. Write triggers so both cases are explicit, for example new.user_id := coalesce(new.user_id, auth.uid()); followed by a clear error if it is still null, instead of silently saving an orphan row.

How to verify

From the app, while signed in, call supabase.auth.getUser() on the same client right before the failing query. If it returns a user but the query still behaves as signed out, the token is not reaching the database; if it returns no user, fix the session handling first.

Related guides

More in Supabase RLS and Storage · all guides