Use auth.admin.listUsers with email filter and pagination fallback

cgen-21d59ae1f2c24144828cf2b095efc953
This commit is contained in:
Builder.io 2025-10-18 00:30:41 +00:00
parent b9bbedce94
commit 7e764b2bf6

View file

@ -125,18 +125,30 @@ export function createServer() {
try { try {
const targetEmail = String(email).trim().toLowerCase(); const targetEmail = String(email).trim().toLowerCase();
// Query auth.users directly with the service role // Prefer GoTrue Admin listUsers; fall back to pagination if email filter unsupported
const { data: user, error } = await (adminSupabase as any) const admin = (adminSupabase as any)?.auth?.admin;
.from("auth.users") if (!admin) {
.select("id, email, email_confirmed_at, confirmed_at") return res.status(500).json({ error: "Auth admin unavailable" });
.eq("email", targetEmail) }
.maybeSingle();
if (error) { let user: any = null;
console.error("[API] auth.users query error:", error); let listResp: any = null;
return res try {
.status((error as any)?.status ?? 500) listResp = await admin.listUsers({ page: 1, perPage: 200, email: targetEmail } as any);
.json({ error: (error as any)?.message || "Query failed" }); } catch (e) {
listResp = null;
}
const initialUsers: any[] = (listResp?.data?.users as any[]) || [];
user = initialUsers.find((u: any) => String(u?.email || '').toLowerCase() === targetEmail) || null;
if (!user) {
// Pagination fallback (limited scan)
for (let page = 1; page <= 5 && !user; page++) {
const resp = await admin.listUsers({ page, perPage: 200 } as any).catch(() => null);
const users = (resp?.data?.users as any[]) || [];
user = users.find((u: any) => String(u?.email || '').toLowerCase() === targetEmail) || null;
}
} }
if (!user) { if (!user) {