Files
JerryandNik Richers 77f33f115b fix(docs): use the OAuth client columns in the audit query (#54098)
## Problem

Fixes #54097.

The OAuth token security guide's audit query uses columns that no longer
exist. `oauth_clients.client_id` was dropped in auth migration
`20250903112500`, and `id` is now the public client ID. The table has
`client_name`, not `name`, and `sessions` stores the client in
`oauth_client_id`, which references `oauth_clients.id`.

The query also filters on `s.created_at`. Sessions last indefinitely and
are refreshed while a user stays active, so a long-lived session
refreshed yesterday is excluded.

## Solution

- Select `oc.id AS client_id` and group by `oc.id`.
- Use `oc.client_name`.
- Join on `s.oauth_client_id = oc.id`.
- Put the 30-day window in the join on `coalesce(s.refreshed_at,
s.created_at)`, so sessions created or refreshed in the last 30 days
count and clients with a count of 0 remain.
- Name the count `users_with_recent_sessions` so it describes the
measure.

This changes one SQL block in one `.mdx` file.

## Verification

Checked against the auth schema from the `supabase/auth` migrations,
including `sessions.refreshed_at`.

Tested on `supabase/postgres:17.6.1.143` with `auth.oauth_clients` and
`auth.sessions` tables that match the current columns. The previous
query fails with `column oc.client_id does not exist`. With a session
created 60 days ago and refreshed yesterday, the creation-time filter
returns `0`, while the refresh-time filter returns `1`. A new session
with no refresh still counts through the `created_at` fallback, and a
client with no sessions returns `0`.

## Review instructions

1. Open `apps/docs/content/guides/auth/oauth-server/token-security.mdx`.
2. Confirm the audit query selects `oc.id AS client_id`, uses
`oc.client_name`, joins on `s.oauth_client_id = oc.id`, and filters on
`coalesce(s.refreshed_at, s.created_at)` inside the join.
3. Run the query against a project with the OAuth server enabled. It
should return every client with the number of users whose session was
created or refreshed in the last 30 days.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)

I used AI to help debug and verify this, but I reviewed the change and
am submitting the PR myself.

---------

Co-authored-by: Nik Richers <nrichers@gmail.com>
2026-12-01 19:41:37 -07:00
..