forked from fahrPool/fahrPool
fix: paginate auth.admin.listUsers() in Nutzerliste query
This commit is contained in:
parent
3013a7d52c
commit
02c9274c13
|
|
@ -10,16 +10,32 @@ export type Nutzer = {
|
|||
gesperrt: boolean;
|
||||
};
|
||||
|
||||
type AdminClient = ReturnType<typeof createAdminClient>;
|
||||
type AuthUser = Awaited<ReturnType<AdminClient["auth"]["admin"]["listUsers"]>>["data"]["users"][number];
|
||||
|
||||
async function listAlleAuthUsers(adminClient: AdminClient): Promise<AuthUser[]> {
|
||||
const alleNutzer: AuthUser[] = [];
|
||||
let page = 1;
|
||||
while (true) {
|
||||
const { data, error } = await adminClient.auth.admin.listUsers({ page, perPage: 1000 });
|
||||
if (error) break;
|
||||
alleNutzer.push(...data.users);
|
||||
if (!data.nextPage) break;
|
||||
page = data.nextPage;
|
||||
}
|
||||
return alleNutzer;
|
||||
}
|
||||
|
||||
export async function listNutzer(): Promise<Nutzer[]> {
|
||||
const supabase = await createClient();
|
||||
const adminClient = createAdminClient();
|
||||
|
||||
const [{ data: profiles }, { data: authData }] = await Promise.all([
|
||||
const [{ data: profiles }, authUsers] = await Promise.all([
|
||||
supabase.from("profiles").select("id, name, role").order("name"),
|
||||
adminClient.auth.admin.listUsers(),
|
||||
listAlleAuthUsers(adminClient),
|
||||
]);
|
||||
|
||||
const authById = new Map((authData?.users ?? []).map((nutzer) => [nutzer.id, nutzer]));
|
||||
const authById = new Map(authUsers.map((nutzer) => [nutzer.id, nutzer]));
|
||||
|
||||
return (profiles ?? []).map((profile) => {
|
||||
const authUser = authById.get(profile.id);
|
||||
|
|
|
|||
Loading…
Reference in New Issue