Search code examples
phplaravelauth0

Laravel auth0 users search returns full list


I have the following piece of code:

$auth0Api = new Management(env('AuthToken'), env('Auth0Domain'));    
$user = $auth0Api->users->search([ "user_id" => "google-oauth2|103122844576645532487"]);

The point of this is to return a single user with the given id

However, whenever I call this I get the full list of my users.

What am I doing wrong?


Solution

  • According to the documentation for Auth0 search they use Lucene Query Syntax, which means an Auth0 search query for google-oauth2 user with the id of 103122844576645532487 should look like this:

    identities.provider:"google-oauth2" AND user_id:"103122844576645532487"
    

    The PHP SDK you are using assembles the request to the API for you under the hood based on key/values provided through a withParam method. The search method you are using takes your parameters (['user_id' => '...']) and passes them to withParam, then your request is executed.

    Your search([...]) code is translating into the following API request:

    /api/v2/users?user_id="google-oauth2|10312284457664553248"
    

    However, the API does not support a user_id parameter on the api/v2/users endpoint and your request is being executed as api/v2/users, which is the request to return all users. This is why you're receiving all of your users: you are not (correctly) passing a search query.

    You need to specifically include the q (query) parameter when building your search, and that parameter should equal a valid Lucene Query.

    $auth0Api->users->search([
        'q' => 'identities.provider:"google-oauth2" AND user_id:"103122844576645532487"'
    ]);
    

    This will search for users where their identity provider is google-oauth2 and their user_id is 103122844576645532487.

    You can read the documentation for the api/v2/users method here.