Files
supabase/apps/docs/content/guides/auth/auth-anonymous/access-control.mdx
Miranda Limonczenko 58b468916c docs(auth): Solve eval for Anonymous sign-in page (#53489)
Closes DOCS-1676

## Problem

The drafted Eval for this guide has one failing check. This PR fixes
that check and also runs the `edit-the-docs` and `test-the-docs` skill
to improve the page as a whole.

The Anonymous Sign-Ins guide tells readers to check the `is_anonymous`
JWT claim in RLS policies. When a guest links an email, phone, or OAuth
identity, the user becomes permanent at once, but the token the client
holds keeps `is_anonymous: true` until it refreshes. A policy on the
claim refuses that new customer with `42501` until then.

## What changed

- **Access control:** a caution on the stale claim, and a policy that
reads `auth.users.is_anonymous` through a `security definer` function in
a non-exposed schema with `search_path = ''`.
- **Convert an anonymous user:** a "Refresh the session after linking"
step with `refreshSession()`.
- **Whole-page edit**, one commit per change type:
- Style: wording, word list, sentence-case and imperative headings, no
time-anchored text.
- Structure (moves only): Access control before Convert, identity
conflicts under Convert, the Next.js caution under Sign in.
- Technical: the `anon` key uses the `anon` role, not `anonymous`; the
opening caution no longer says to check the JWT claim; the
existing-account sample throws instead of a top-level `return`.

Renamed headings with no inbound links: Resolving identity conflicts,
Linking an anonymous user to an existing account, Automatic cleanup.
`#access-control` (Studio) and `#link-an-email--phone-identity` are
unchanged.

## Eval on the preview

`build-docs-016-auth-anonymous` with `REFERENCE` pointed at the preview,
3 runs ([workflow
run](https://github.com/supabase/evals/actions/runs/43983385855)).

| Run | Score | Guest who just made an account can post a review |
Failed check |
| --- | --- | --- | --- |
| Baseline (published page) | 9/10 in all 3 runs | Fail in all 3 | Guest
who just made an account can post a review |
| 1 | 10/10 | Pass | None |
| 2 | 9/10 | Pass | Agent read the guide |
| 3 | 9/10 | Pass | Agent read the guide |

All 3 runs fetched the preview and wrote the `security definer` policy.
The guide-read check only counts fetches from `supabase.com`, so it
can't pass on a preview. Run 1 passed it because that agent also opened
the published page.

## Manual testing

1. Open the [Anonymous sign-ins
preview](https://docs-git-docs-anonymous-claim-refresh-supabase.vercel.app/docs/guides/auth/auth-anonymous).
2. Check the new caution and function example under **Access control**,
and the new **Refresh the session after linking** section.
3. Click the anchor links between the two sections and to the RLS guide.
2026-12-01 10:13:18 -08:00

88 lines
4.0 KiB
Plaintext

---
id: 'auth-anonymous-access-control'
title: 'Restrict anonymous user access'
description: '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](/dashboard/project/_/sql) in the Supabase Dashboard or with `psql` over a direct connection. To connect with `psql`, see [Direct connection](/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 );
```
<Admonition type="caution" title="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](/docs/guides/auth/auth-anonymous/convert#refresh-the-session-after-linking)
- Checking `is_anonymous` in `auth.users` instead of the claim, as shown below
</Admonition>
## Check the current state instead
To check the current state, read `is_anonymous` from `auth.users` through a [`security definer` function](/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](/dashboard/project/_/sql) in the Supabase Dashboard or with `psql` over a direct connection. To connect with `psql`, see [Direct connection](/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.
<Admonition type="note" title="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.
</Admonition>
## Next steps
To keep going, use these pages:
- [Convert an anonymous user](/docs/guides/auth/auth-anonymous/convert): link an identity and refresh the session.
- [Row Level Security](/docs/guides/database/postgres/row-level-security): write policies for your other tables.