Gotrue-js Get all users returns a maximum of 50?

Hey dude!

I can’t seem to find the prior post where someone else asked about the same issue and I directed them to the somewhat-undocumented page parameter available on all GoTrue endpoints. The tl;dr: here is that I wouldn’t use the gotrue-js library for admin-level requests you’re running from functions. I know it supports them (sorta?) but still, it’ll be easier for you if you just manually setup your request in the function with a fetch (using node-fetch, really). Here’s an example from my React-GoTrue integration demo site, gatsby-identity-demo (all code here), that runs an admin-level action (adding/removing a role from a user) against the site’s GoTrue instance.

I’d recommend giving that a full read, but essentially it just runs a PUT against the GoTrue instance, whose hostname/URL you can get from the context.

Anyway, pagination. What you’re seeing is page 1 of the results, and the page size is capped to 50 per page by default. You have two options. First, you could just request pages 2, then 3, etc. until you get all the results. How many results are there? You should be receiving a header in the response with key X-Total-Count that tells you, and I think another header with key Link that would get you to the next or last page. Not sure which, you may need to play with it.

You can also just specify a bigger page size, but that has performance implications too. If you have a thousand users and tell GoTrue to just send back all thousand in one page, you may hit timeouts. You just need to add a query-string parameter of per_page=xxx, so {identityHostEtc..}/admin/users?per_page=100.

Hope that helps! As always, YMMV :+1:t2:


Jon


EDIT: Found it:


EDIT2: Thanks for the plug on the react library I wrote (missed that earlier) — it does indeed work great for end users but it’s not made to execute admin-level commands. And nothing on the user-side should ever need pagination, I believe. Since admin-level commands really are only meant to be run in (Authorized) Netlify Functions, which run in a Node runtime, my library wouldn’t even work there. It’s running as expected! :grinning:

2 Likes