Restrict anonymous user access
Restrict what anonymous users can do in your database by telling them apart from permanent users in row-level security (RLS) policies.
Check the claim
An anonymous user uses the authenticated role, like a permanent user. To tell them apart in row-level security (RLS) policies, check the is_anonymous claim in the JWT that auth.jwt() returns.
Run the following SQL using the SQL Editor in the Supabase Dashboard or with psql over a direct connection. To connect with psql, see Direct connection.
create policy "Only permanent users can post to the news feed"on news_feed as restrictive for insertto authenticatedwith check ((select (auth.jwt()->>'is_anonymous')::boolean) is false );create policy "Permanent users can post to the news feed"on news_feed for insertto authenticatedwith check ( true );create policy "Anonymous and permanent users can view the news feed"on news_feed for selectto authenticatedusing ( true );Linked users stay anonymous until the token refreshes
If your policies check the is_anonymous claim, a guest who links an email, phone, or OAuth identity is still treated as anonymous until their session refreshes, which can take up to the JWT expiry time. Until then, their requests fail with error 42501. Avoid this by either:
- Refreshing the session after linking
- Checking
is_anonymousinauth.usersinstead of the claim, as shown below
Check the current state instead
To check the current state, read is_anonymous from auth.users through a security definer function. Create the function in a schema the Data API doesn't expose, such as private, and set search_path = ''.
Run the following SQL using the SQL Editor in the Supabase Dashboard or with psql over a direct connection. To connect with psql, see Direct connection.
create schema if not exists private;create function private.is_permanent_user()returns booleanlanguage sqlstablesecurity definerset search_path = ''as $$ select exists ( select 1 from auth.users where id = (select auth.uid()) and is_anonymous is false );$$;revoke all on function private.is_permanent_user() from public;grant usage on schema private to authenticated;grant execute on function private.is_permanent_user() to authenticated;alter policy "Only permanent users can post to the news feed"on news_feedwith check ( (select private.is_permanent_user()) );The alter policy statement replaces the claim check in the policy from the previous example, so stale tokens no longer fail. For a fresh setup, create the policy with create policy ... as restrictive for insert and the same with check expression, and add the permissive insert policy from the previous example.
Use restrictive policies
RLS policies are permissive by default, and Postgres combines permissive policies with OR. Make the anonymous-user check a restrictive policy so other policies can't bypass it.
A restrictive policy on its own allows nothing. Pair it with at least one permissive policy for the same operation that returns true.
Next steps
To keep going, use these pages:
- Convert an anonymous user: link an identity and refresh the session.
- Row Level Security: write policies for your other tables.