# Auth helpers in SQL
Source: https://sovaria.in/docs/auth-helpers

auth.uid(), auth.role(), auth.email(), auth.verified_email(), auth.has_role() and auth.jwt(): what each returns for anonymous, signed-in and server callers, and how to use them in row-level security.

Every project database has these functions in the `auth` schema. They read who is calling the current request — Sovaria sets that per request — so you can use them in row-level security policies, defaults, views and your own functions. The names match Supabase's, so policies you bring over keep working.

| Function | Returns | Anonymous (anon key) | Signed-in app user | Service role key / console |
|---|---|---|---|---|
| `auth.uid()` | `uuid` | `null` | The user's id | `null` unless a user is set |
| `auth.role()` | `text` | `anon` | `authenticated` | `service_role` |
| `auth.email()` | `text` | `null` | The user's email | `null` |
| `auth.email_verified()` | `boolean` | `false` | `true` once the user has confirmed their email | `false` |
| `auth.verified_email()` | `text` | `null` | The user's email once it is confirmed, else `null` | `null` |
| `auth.has_role(role_name text)` | `boolean` | `false` | `true` if the user has that app role (case-insensitive) | `false` |
| `auth.jwt()` | `jsonb` | The request's claims (`role`, …) | All claims: `sub`, `email`, `role`, `app_roles`, … (`is_anonymous: true` for an anonymous app user) | Claims with `role: service_role` |

An anonymous app user has an id, so `auth.uid()` policies treat them like any user. To keep them out of something until they sign up, add `and coalesce((auth.jwt()->>'is_anonymous')::boolean, false) = false`.

> **Note:** Custom API keys act like the anon key in SQL: `auth.role()` is `anon` and `auth.uid()` is `null`. Give them what they need with access policies, not with `auth.role()` checks.

## auth.uid()

The signed-in user's id — the same id as in the Users page and in `sovaria.auth.getUser()`. Store it in an owner column and compare against it:

SQL:
```sql
create table notes (
  id bigint generated always as identity primary key,
  owner_id uuid not null default auth.uid(),   -- filled in from the caller
  body text not null
);
alter table notes enable row level security;
alter table notes force row level security;

create policy "own notes" on notes
  for all using (owner_id = auth.uid()) with check (owner_id = auth.uid());
```

For anonymous requests `auth.uid()` is `null`, and `owner_id = null` is never true in SQL, so visitors see nothing — no extra check needed.

> **Tip:** So your team (Table Editor, exports) and the service role key still see every row of a forced table, add `or current_setting('app.rls_bypass', true) = 'on'` to the `using` and `with check` expressions — Sovaria sets it only for them.

## auth.role()

`anon`, `authenticated` or `service_role`. Use it to separate visitors from signed-in users:

SQL:
```sql
create policy "signed-in users read" on articles
  for select using (auth.role() = 'authenticated');
```

## auth.has_role()

True when the signed-in user has an **app role** you assigned on the Users page (or with `sovaria.auth.admin…`, `sov auth-product users` and `sov auth-product roles-set` / `roles-add`). Role names compare case-insensitively.

SQL:
```sql
create policy "staff manage products" on products
  for all using (auth.has_role('STAFF')) with check (auth.has_role('STAFF'));

create policy "anyone reads products" on products
  for select using (true);
```

## auth.email() and auth.jwt()

`auth.email()` is the address the user signed up with. Until they confirm it, it is only what they typed, so a stranger can sign up with someone else's address. When a policy gives people rows by email (a client's deals, a customer's orders), use `auth.verified_email()` instead: it is `null` until the address is confirmed. Turn on **Email verification** in the project's Authentication settings so users get the confirmation email.

SQL:
```sql
create policy "customers read theirs" on orders
  for select using (email = auth.verified_email());
```

`auth.jwt()` returns every claim as `jsonb` — useful for custom claims your auth hook adds ([Authentication settings reference](https://sovaria.in/docs/auth-settings)):

SQL:
```sql
-- Custom claim "tenant_id" added by your JWT hook
create policy "same tenant" on invoices
  for select using (tenant_id = (auth.jwt() ->> 'tenant_id')::uuid);
```

> **Warning:** Only trust claims Sovaria or your own hook put in the token. Never base access on values the client sends in the request body.

## Good to know

- The helpers are `STABLE`: Postgres evaluates them once per statement, so they are cheap even in policies on large tables. Index the columns you compare them with (`owner_id`).
- They describe the request, not the database user. In a direct database connection ([Database: connection, compute, disk and backups](https://sovaria.in/docs/settings-database)) there is no request, so `auth.uid()` is `null`.
- Inside a `security definer` function they still describe the caller, which lets you write helper functions that check ownership.
