Admin Users Count

Hi. I am currently trying to get just the count of admin users. My query looks like the following:
// Admin count query - separate optimized query just for counting
const { data: adminData } = useQuery<UsersResponse>({
queryKey: ["users-admin-count"],
queryFn: async () => {
try {
const data = await client.admin.listUsers(
{
query: {
limit: 1, // We only need the total count, not the actual users
filterField: "role",
filterOperator: "eq",
filterValue: "admin"
}
},
{
throw: true,
},
);
return data as UsersResponse;
} catch (error: any) {
console.error("Failed to fetch admin count:", error);
return { users: [], total: 0 } as UsersResponse;
}
},
// Keep the count cached for longer since it doesn't change as frequently
staleTime: 5 * 60 * 1000, // 5 minutes
});
// Admin count query - separate optimized query just for counting
const { data: adminData } = useQuery<UsersResponse>({
queryKey: ["users-admin-count"],
queryFn: async () => {
try {
const data = await client.admin.listUsers(
{
query: {
limit: 1, // We only need the total count, not the actual users
filterField: "role",
filterOperator: "eq",
filterValue: "admin"
}
},
{
throw: true,
},
);
return data as UsersResponse;
} catch (error: any) {
console.error("Failed to fetch admin count:", error);
return { users: [], total: 0 } as UsersResponse;
}
},
// Keep the count cached for longer since it doesn't change as frequently
staleTime: 5 * 60 * 1000, // 5 minutes
});
but the response looks like the following although the docs state that the "total" in the meta data is the count after the filters:
{
"users": [
{
"id": "****",
"name": "***",
"email": "***",
"emailVerified": ***,
"image": "***",
"createdAt": "****",
"updatedAt": "****",
"role": "admin",
"banned": null,
"banReason": null,
"banExpires": null
}
],
"total": 6,
"limit": 1
}
{
"users": [
{
"id": "****",
"name": "***",
"email": "***",
"emailVerified": ***,
"image": "***",
"createdAt": "****",
"updatedAt": "****",
"role": "admin",
"banned": null,
"banReason": null,
"banExpires": null
}
],
"total": 6,
"limit": 1
}
Any insight into this would be awesome.
6 Replies
yoyojoe
yoyojoeOP2d ago
Here is a snip of the admin dashboard I am creating
No description
Lick A Brick
Lick A Brick2d ago
What are you expecting as the output? You are querying users with the role 'admin', as per your response you have 6 users matching this filter? You are only seeing 1 user because you set the limit to 1. Limit != Filter.
yoyojoe
yoyojoeOP2d ago
Whats up @Lick A Brick. I'm expecting the total to be equal to the number of users with the given filter criteria. I only have 1 admin user so I would expect "total":6 Right now its returning the total number of all users
Lick A Brick
Lick A Brick2d ago
Hmm... the docs seem kinda confusing to me tbh. At https://www.better-auth.com/docs/plugins/admin#list-users it mentions the parameter filter which accepts an array of objects, but in the example it shows filterField, filterOperator and filterValue as seperate parameters. Should't it be filter: [{ field: "role", operator: "eq", value: "admin"}] what happens if you remove the limit: 1 does it return all 6 users or does it still only return the single admin user?
yoyojoe
yoyojoeOP20h ago
Sorry. Just realized I didn’t respond. I did try this but got a type error. I looked into it further and filter isn’t a query option. I’m going to update and see if that changes anything
Netrifier
Netrifier19h ago
Can you share which adapter you are using and its configuration?

Did you find this page helpful?