@@ -26,7 +26,7 @@ import {
2626 OAuthClientResponse ,
2727 OAuthClientListResponse ,
2828} from './lib/types'
29- import { AuthError , isAuthError } from './lib/errors'
29+ import { AuthError , isAuthError , AuthApiError } from './lib/errors'
3030
3131export default class GoTrueAdminApi {
3232 /** Contains all MFA administration methods. */
@@ -194,22 +194,31 @@ export default class GoTrueAdminApi {
194194 * Get a list of users.
195195 *
196196 * This function should only be called on a server. Never expose your `service_role` key in the browser.
197- * @param params An object which supports `page` and `perPage` as numbers, to alter the paginated results.
197+ * @param params An object which supports `page` and `perPage` as numbers, to alter the paginated results,
198+ * and `filter` string to search for users by email address.
199+ *
200+ * @warning The filter parameter is provided for convenience but may have performance implications on large databases.
201+ * Consider using pagination without filters for projects with many users.
198202 */
203+
199204 async listUsers (
200- params ?: PageParams
205+ params ?: PageParams & { filter ?: string }
201206 ) : Promise <
202207 | { data : { users : User [ ] ; aud : string } & Pagination ; error : null }
203208 | { data : { users : [ ] } ; error : AuthError }
204209 > {
205210 try {
211+ if ( params ?. filter && params . filter . trim ( ) . length < 3 ) {
212+ throw new AuthApiError ( 'Filter must be at least 3 characters' , 400 , 'invalid_request' )
213+ }
206214 const pagination : Pagination = { nextPage : null , lastPage : 0 , total : 0 }
207215 const response = await _request ( this . fetch , 'GET' , `${ this . url } /admin/users` , {
208216 headers : this . headers ,
209217 noResolveJson : true ,
210218 query : {
211219 page : params ?. page ?. toString ( ) ?? '' ,
212220 per_page : params ?. perPage ?. toString ( ) ?? '' ,
221+ ...( params ?. filter ? { filter : params . filter } : { } ) ,
213222 } ,
214223 xform : _noResolveJsonResponse ,
215224 } )
@@ -220,8 +229,13 @@ export default class GoTrueAdminApi {
220229 const links = response . headers . get ( 'link' ) ?. split ( ',' ) ?? [ ]
221230 if ( links . length > 0 ) {
222231 links . forEach ( ( link : string ) => {
223- const page = parseInt ( link . split ( ';' ) [ 0 ] . split ( '=' ) [ 1 ] . substring ( 0 , 1 ) )
224- const rel = JSON . parse ( link . split ( ';' ) [ 1 ] . split ( '=' ) [ 1 ] )
232+ const [ urlPart , relPart ] = link . split ( ';' ) . map ( ( part ) => part . trim ( ) )
233+ const url = urlPart . slice ( 1 , - 1 ) // Remove the leading "</" and trailing ">"
234+
235+ const searchParams = new URLSearchParams ( url . split ( '?' ) [ 1 ] )
236+ const page = parseInt ( searchParams . get ( 'page' ) || '1' , 10 )
237+ const rel = relPart . split ( '=' ) [ 1 ] . replace ( / " / g, '' )
238+
225239 pagination [ `${ rel } Page` ] = page
226240 } )
227241
@@ -414,9 +428,7 @@ export default class GoTrueAdminApi {
414428 *
415429 * This function should only be called on a server. Never expose your `service_role` key in the browser.
416430 */
417- private async _createOAuthClient (
418- params : CreateOAuthClientParams
419- ) : Promise < OAuthClientResponse > {
431+ private async _createOAuthClient ( params : CreateOAuthClientParams ) : Promise < OAuthClientResponse > {
420432 try {
421433 return await _request ( this . fetch , 'POST' , `${ this . url } /admin/oauth/clients` , {
422434 body : params ,
@@ -465,17 +477,12 @@ export default class GoTrueAdminApi {
465477 */
466478 private async _deleteOAuthClient ( clientId : string ) : Promise < OAuthClientResponse > {
467479 try {
468- return await _request (
469- this . fetch ,
470- 'DELETE' ,
471- `${ this . url } /admin/oauth/clients/${ clientId } ` ,
472- {
473- headers : this . headers ,
474- xform : ( client : any ) => {
475- return { data : client , error : null }
476- } ,
477- }
478- )
480+ return await _request ( this . fetch , 'DELETE' , `${ this . url } /admin/oauth/clients/${ clientId } ` , {
481+ headers : this . headers ,
482+ xform : ( client : any ) => {
483+ return { data : client , error : null }
484+ } ,
485+ } )
479486 } catch ( error ) {
480487 if ( isAuthError ( error ) ) {
481488 return { data : null , error }
0 commit comments