TheQ
TheQ

Reputation: 2019

How to check existing user in cognito - android?

so i have aws cognito set up in android. And i want to perform a check to see when the user enters a username, that that username doesn't already exist in my cognito pool.

Currently i have this:

 cognitoUser = userPool.getUser(username_ET.getText().toString());
 if(cognitoUser.getUserId().isEmpty())
 {
      //Great, this is a new user.
 }

But, cognitoUser just gets set to whatever i put in, it doesn't go back in check.

Hope someone can help!

Upvotes: 0

Views: 3812

Answers (4)

goWithSwagger
goWithSwagger

Reputation: 1772

ListUsers now makes this much easier.

https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html

Here is a simple .NET example:

        Dim userRequest = New ListUsersRequest With {
                .UserPoolId = userPoolId,
                .Filter = "username = JohnDoe"
            }
        Dim response = client.ListUsers(userRequest)
        If response.Users.Count < 1 Then
            Return False
        End If
        Return True

Upvotes: 1

hannibal1296
hannibal1296

Reputation: 15

This will help

catch(Exception exception){
    if(exception.getMessage().contains("UserNotFoundException")){
        ...
    }
}

Upvotes: 0

TheQ
TheQ

Reputation: 2019

The answer to my question is:

        SignUpHandler signupCallback = new SignUpHandler() {

            @Override
            public void onSuccess(CognitoUser cognitoUser, boolean userConfirmed, CognitoUserCodeDeliveryDetails cognitoUserCodeDeliveryDetails)
            {
                Log.d(COGNITO_REGISTER, "sign up succeeded!");

            }
            @Override
            public void onFailure(Exception exception)
            {
                Log.d(COGNITO_REGISTER, "sign up failed!");
                Log.d(COGNITO_REGISTER, exception.getMessage());

                // Sign-up failed, check exception for the cause
            }
        };
        userPool.signUpInBackground(username_ET.getText().toString(), password_ET.getText().toString(), userAttributes, null, signupCallback);

Upvotes: -1

Ionut Trestian
Ionut Trestian

Reputation: 5751

If you try signing in with a username that doesn't exist, you will get a UserNotFoundException exception.

Upvotes: 1

Related Questions