Auth0 Java Management API listByEmail() with a userFilter

  • Which SDK does this apply to? auth0-java
  • Which verison of the SDK you are using? 1.13.3
  • Which version of the platform are you facing this error on? Java 8

This is the JAVA code in question. I’m trying to find a user by a combination of email and connection. We do have the same email address registered under multiple applications/connections. I need this code to return the 1 email/connection combination that matches. As it is, it is returning every matching email address, regardless of the connection.

 public User findUser(String email) {
        try {
            ManagementAPI managementAPI = buildManagementApi();
            String query = "identities.connection:\"" + getConnection() +"\"";
            UserFilter userFilter = new UserFilter().withQuery(query);
            List<User> users = managementAPI.users().listByEmail(email, userFilter).execute();
            return Iterables.getOnlyElement(users);
        } catch(Auth0Exception e) {
            throw new AuthServiceException(e);
        }
    }

When logged, the query becomes: identities.connection:“MyApp-Localhost”…in case you were wondering if getConnection() returns the correct value.

I’m building the ManagementAPI object elsewhere, but it is working fine. The query part is just being completely ignored. If someone can suggest a better way (with the auth0-java library) I’m open to suggestions…or if someone knows what I’m doing wrong here, that’d be great as well. FYI, I attempted using userFilter.withSearchEngine(“v3”), but got nowhere with that. It doesn’t seem to have any effect either.

Hi @brandon.wedgeworth,

I am not a Java expert, but I can ask to get a second set of eyes on this. You aren’t receiving any errors from your app?

Thanks,
Dan

No errors from Auth0. I do get an error on the “return Iterables.getOnlyElement(users);” line, but that’s because I’m getting back a list of 3 users (same email address but different connection) when I’m expecting only 1.

Thanks.

Are your users accounts linked?

No, they’re not linked. For the time being, we have separate connections/applications for a couple dev and test environments and the users need to remain separated.

Hi @brandon.wedgeworth,

Could you DM me the name of your tenant? And could you share the getConnection() function?

Thanks,
Dan

Hi @brandon.wedgeworth
I’m afraid that might be a design error for the Java API, as the /users-by-email endpoint does not currently provide any real filtering abilities (other than the email address). The only filter available would be the withFields filter for the fields parameter available in that endpoint, that lets you select which fields should be returned in the response. See Auth0 Management API v2.

Any additional filtering would have to occur on the client side if you use that endpoint (which is optimized for email searches).

1 Like

@nicolas_sabena Ok, then I have two options. Figure out how to filter a list of users returned by the connection they belong to (I couldn’t see how to do this), or figure out how to make a different JAVA call to get a user by a combination of either username or email and connection.

@nicolas_sabena Can you please provide some insight as to why the following yields no results when searching for the same email address as my other code which returns 2 results?

public User findUser(String email) {
    try {
        ManagementAPI managementAPI = buildManagementApi();
        String query = "identities.connection:\"" + getConnection() +"\" AND email:\"" + email +"\"";
        UserFilter userFilter = new UserFilter().withQuery(query).withPage(1,5);
        UsersPage response = managementAPI.users().list(userFilter).execute();
        return Iterables.getOnlyElement(response.);
    } catch(Auth0Exception e) {
        throw new AuthServiceException(e);
    }
}

Or, possibly help me figure out how to correctly filter the results from listByEmail by connection. I’m assuming I should be able to do it somehow via getIdentities() on the individual users returned in the list…

@nicolas_sabena Nevermind, I got filtering on my side working using getIdentities().

2 Likes

Glad you got this working!

Thanks,
Dan

Hey @brandon.wedgeworth sorry about the delay coming back to you. Indeed, the identities array (with the connection property) will help you filter out identities based on the connection name.

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.