Files
supabase/apps/studio/lib/api/self-hosted/auth-senders.ts
Alaister YoungandAlaister Young be32a941cd [FE-5114] (part 4) – refactor(studio): migrate platform auth and misc routes (#54031)
## Problem

Studio's `routes/api/platform/auth/**` and platform misc/stub routes
still run legacy Next Pages Router handlers through
`compat/next/api.ts`, and 4 legacy database/props routes have no callers
left. Part 4 of 7 for
[FE-5114](https://linear.app/supabase/issue/FE-5114/rewrite-api-routes-as-native-tanstack-start-handlers-and-remove-the)
migrates the 16 active routes to native TanStack Start handlers and
removes the 4 unused ones.

## Solution

**Changed:**

- Migrated `platform/auth/$ref/{invite,magiclink,otp,recover}` to
one-line `createAuthSenderHandlers` routes, and `users/index`,
`users/$id/index`, and `users/$id/factors` to `createApiHandlers`,
preserving the FE-5124 fixes (`Promise.all` MFA factor deletion, `code
?? 500` on auth senders, `Allow: PATCH, DELETE` on `users/$id`, now
derived by `createApiHandlers`)
- Migrated 9 platform misc/stub routes (`deployment-mode`,
`integrations/{$slug,github/*}`,
`organizations/{index,$slug/billing/subscription}`, `profile/index`, and
`telemetry/event`)
- Moved the `FETCH_TYPE_BYPASS_EXCEPTIONS` entries for the four sender
routes in `eslint.config.cjs` to `lib/api/self-hosted/auth-senders.ts`

**Added:**

- `createAuthSenderHandlers(endpoint, field)` in
`lib/api/self-hosted/auth-senders.ts`: shared POST handler for the
self-hosted GoTrue `invite`, `magiclink`, `otp`, and `recover` endpoints
(forwards only `email` or `phone`, uses `constructHeaders` with the
service role key, and passes upstream errors through with `code ?? 500`)
- Regression tests for `auth/$ref/users/$id/factors`,
`auth/$ref/users/$id`, and all four auth sender routes

**Removed:**

- Removed 4 unused routes (`platform/database/$ref/pooling`,
`platform/props/org/$slug`, `platform/props/project/$ref/index`, and
`platform/props/project/$ref/api`) and regenerated `routeTree.gen.ts`

## Review instructions

1. Run `pnpm --filter studio exec vitest run
tests/routes/api/platform/auth/`.
2. On self-hosted Studio, invite/ban/delete a user, remove MFA factors
for a user, and confirm `GET /api/platform/auth/default/users/<id>`
returns `405` with `Allow: PATCH, DELETE`.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] 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)

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-12-01 22:30:19 +08:00

36 lines
1.3 KiB
TypeScript

import { fetchPost } from '@/data/fetchers'
import { constructHeaders, parseJsonBody } from '@/lib/api/apiHelpers'
import { createApiHandlers } from '@/lib/api/apiWrapper'
/**
* POST handler for the self-hosted GoTrue endpoints that send an email or SMS (invite, magic
* link, OTP, password recovery). Forwards only `field` from the request body, authenticated
* with the service role key, and passes upstream errors through with their status code.
*/
export function createAuthSenderHandlers(
endpoint: 'invite' | 'magiclink' | 'otp' | 'recover',
field: 'email' | 'phone'
) {
return createApiHandlers({
POST: async ({ request }) => {
const body = await parseJsonBody<Partial<Record<typeof field, string>>>(request)
const headers = constructHeaders({
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${process.env.SUPABASE_SERVICE_KEY}`,
})
const response = await fetchPost(
`${process.env.SUPABASE_URL}/auth/v1/${endpoint}`,
{ [field]: body[field] },
{ headers }
)
if (response.error) {
const { code, message } = response.error
return Response.json({ message }, { status: code ?? 500 })
}
return Response.json(response)
},
})
}