Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/GoTrueAdminApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,31 @@ export default class GoTrueAdminApi {
* Get a list of users.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
* @param params An object which supports `page` and `perPage` as numbers, to alter the paginated results.
* @param params An object which supports `page` and `perPage` as numbers, to alter the paginated results,
* and `filter` string to search for users by email address.
*
* @warning The filter parameter is provided for convenience but may have performance implications on large databases.
* Consider using pagination without filters for projects with many users.
*/
async listUsers(
params?: PageParams
params?: PageParams & { filter?: string }
): Promise<
| { data: { users: User[]; aud: string } & Pagination; error: null }
| { data: { users: [] }; error: AuthError }
> {
try {
if (params?.filter && params.filter.trim().length < 3) {
throw new AuthApiError('Filter must be at least 3 characters', 400);
}

const pagination: Pagination = { nextPage: null, lastPage: 0, total: 0 }
const response = await _request(this.fetch, 'GET', `${this.url}/admin/users`, {
headers: this.headers,
noResolveJson: true,
query: {
page: params?.page?.toString() ?? '',
per_page: params?.perPage?.toString() ?? '',
...(params?.filter ? { filter: params.filter } : {}),
},
xform: _noResolveJsonResponse,
})
Expand All @@ -203,8 +212,13 @@ export default class GoTrueAdminApi {
const links = response.headers.get('link')?.split(',') ?? []
if (links.length > 0) {
links.forEach((link: string) => {
const page = parseInt(link.split(';')[0].split('=')[1].substring(0, 1))
const rel = JSON.parse(link.split(';')[1].split('=')[1])
const [urlPart, relPart] = link.split(';').map(part => part.trim());
const url = urlPart.slice(1, -1); // Remove the leading "</" and trailing ">"

const searchParams = new URLSearchParams(url.split('?')[1]);
const page = parseInt(searchParams.get('page') || '1', 10);
const rel = relPart.split('=')[1].replace(/"/g, '');

pagination[`${rel}Page`] = page
})

Expand Down
22 changes: 22 additions & 0 deletions test/GoTrueApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ describe('GoTrueAdminApi', () => {
expect(emails.length).toBeGreaterThan(0)
expect(emails).toContain(email)
})

test('listUsers() should support filter property', async () => {
const { email } = mockUserCredentials()
const { error: createError, data: createdUser } = await createNewUserWithEmail({ email })
expect(createError).toBeNull()
expect(createdUser.user).not.toBeUndefined()

const { error: listUserError, data: userList } = await serviceRoleApiClient.listUsers({
filter: email,
})
expect(listUserError).toBeNull()
expect(userList).toHaveProperty('users')
expect(userList).toHaveProperty('aud')

const emails =
userList.users?.map((user: User) => {
return user.email
}) || []

expect(emails.length).toEqual(1)
expect(emails).toContain(email)
})

test('listUsers() returns AuthError when page is invalid', async () => {
const { error, data } = await serviceRoleApiClient.listUsers({
Expand Down