fix(admin): retry GoTrue admin reads on transient empty-body responses (bulk-load robustness)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { type NextRequest } from 'next/server';
|
||||
import { requireAdminApi } from '@/lib/auth/admin-guard';
|
||||
import { getSupabaseAdmin } from '@/lib/supabase/admin';
|
||||
import { withAdminRetry } from '@/lib/admin/retry';
|
||||
import { logAdminAction } from '@/lib/auth/audit';
|
||||
import { isUuid } from '@/lib/admin/validators';
|
||||
import { jsonNoStore } from '@/lib/admin/response';
|
||||
@@ -33,8 +34,12 @@ export async function GET(
|
||||
|
||||
const admin = getSupabaseAdmin();
|
||||
|
||||
const { data: userRes, error: userErr } =
|
||||
await admin.auth.admin.getUserById(id);
|
||||
// Retry transient empty-body GoTrue responses so a burst-induced flake isn't
|
||||
// misreported as a 404 for a user that actually exists. A genuine not-found
|
||||
// (non-transient) still falls through to the clean 404 below.
|
||||
const { data: userRes, error: userErr } = await withAdminRetry(() =>
|
||||
admin.auth.admin.getUserById(id),
|
||||
);
|
||||
if (userErr || !userRes.user) {
|
||||
return jsonNoStore({ error: 'user not found' }, { status: 404 });
|
||||
}
|
||||
@@ -103,9 +108,11 @@ export async function DELETE(
|
||||
// Confirm the user exists up front. GoTrue replies with an empty body when
|
||||
// deleting a non-existent user, which supabase-js surfaces as an opaque
|
||||
// JSON-parse error (no status / "not found" text); a positive existence
|
||||
// check lets us return a clean 404 instead of a misleading 500.
|
||||
const { data: existing, error: lookupErr } =
|
||||
await admin.auth.admin.getUserById(id);
|
||||
// check lets us return a clean 404 instead of a misleading 500. Retry the
|
||||
// lookup so a transient empty body under load isn't mistaken for not-found.
|
||||
const { data: existing, error: lookupErr } = await withAdminRetry(() =>
|
||||
admin.auth.admin.getUserById(id),
|
||||
);
|
||||
if (lookupErr || !existing.user) {
|
||||
return jsonNoStore({ error: 'user not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user