> Note for AI agents: For other pages, see [llms.txt](https://supabase.com/docs/llms.txt).

# Restrict anonymous user access

Tell anonymous users apart from permanent users in row-level security policies, and avoid blocking a user who just linked an email, phone, or OAuth identity.

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](https://supabase.com/dashboard/project/_/sql) in the Supabase Dashboard or with `psql` over a direct connection. To connect with `psql`, see [Direct connection](https://supabase.com/docs/guides/database/connecting-to-postgres#direct-connection).

```sql
create policy "Only permanent users can post to the news feed"
on news_feed as restrictive for insert
to authenticated
with check ((select (auth.jwt()->>'is_anonymous')::boolean) is false );

create policy "Permanent users can post to the news feed"
on news_feed for insert
to authenticated
with check ( true );

create policy "Anonymous and permanent users can view the news feed"
on news_feed for select
to authenticated
using ( true );
```

Caution: 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](https://supabase.com/docs/guides/auth/auth-anonymous/convert#refresh-the-session-after-linking)
- Checking `is_anonymous` in `auth.users` instead 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](https://supabase.com/docs/guides/database/postgres/row-level-security#use-security-definer-functions). 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](https://supabase.com/dashboard/project/_/sql) in the Supabase Dashboard or with `psql` over a direct connection. To connect with `psql`, see [Direct connection](https://supabase.com/docs/guides/database/connecting-to-postgres#direct-connection).

```sql
create schema if not exists private;

create function private.is_permanent_user()
returns boolean
language sql
stable
security definer
set 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_feed
with 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.

Note: 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](https://supabase.com/docs/guides/auth/auth-anonymous/convert): link an identity and refresh the session.
- [Row Level Security](https://supabase.com/docs/guides/database/postgres/row-level-security): write policies for your other tables.
