Aws-sdk-android: To check email exists or not , while calling forgot password

Created on 27 Dec 2019  路  14Comments  路  Source: aws-amplify/aws-sdk-android

State your question
While clicking on Forgot password we called below code, we are getting state as CONFIRMATION_CODE for all the emails.
_How can we capture , if user is not exists in the system.
Is there any other function to be called before, to check verify the user?_

Which AWS Services are you utilizing?
_COGNITO_

Provide code snippets (if applicable)

AWSMobileClient.getInstance() .forgotPassword(email, object : Callback<ForgotPasswordResult?> {
                            override fun onResult(result: ForgotPasswordResult?) {
                                runOnUiThread {
                                    if (result != null) {
                                        when (result.state) {
                                            ForgotPasswordState.CONFIRMATION_CODE -> {
                                              //Navigation to Confirmation code alert
                                            }
                                            else -> showSnackBar("Unable to process")
                                        }
                                    }
                                }
                            }
                         override fun onError(e: Exception?) {
                                showSnackBar("Error Occurred")
                            }
                        })

Environment(please complete the following information):
implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.16.5'

AWSMobileClient Needs Info from Requester Usage Question

Most helpful comment

Hello @TrekSoft
I managed to solve the problem. There is a setting in the Cognito console that was checked by default. I changed it to Legacy and it worked.

in Cognito Console --> App Clients --> Show Details

Prevent User Existence Errors
[ ] Legacy // <-- this one works
[ ] Enabled (Recommended) //was checked by default

All 14 comments

Hey @arasu33 , if you call AWSMobileClient.getInstance().forgotPassword() function with an invalid username you should get a UserNotFoundException.

Are you saying you've tested calling that with an email you're sure is not registered and it is still going into onResult and giving a ConfirmationCode state and not throwing that exception?

Hey @arasu33 , if you call AWSMobileClient.getInstance().forgotPassword() function with an invalid username you should get a UserNotFoundException.

Are you saying you've tested calling that with an email you're sure is not registered and it is still going into onResult and giving a ConfirmationCode state and not throwing that exception?

Yes, I called that AWSMobileClient.getInstance().forgotPassword() function with Invalid Email Id, but i got that ConfirmationCode state ... No exception was thrown

Any updates on this or any thing to be modified in the above code to get user not found exception. Still this issue persist from my end.

image

I used non existing user, but i got to Confirmation code state. Sample Screen shot attached
Used com.amazonaws:aws-android-sdk-mobile-client:2.16.6

forgotPassword should always throw an exception "Username/clientID combination not found".
Are you sure you are using a non registered email?
```
AWSMobileClient.getInstance().forgotPassword(username, new Callback() {
@Override
public void onResult(final ForgotPasswordResult result) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("STATUS", "forgot password state: " + result.getState());
switch (result.getState()) {
case CONFIRMATION_CODE:
<>
break;
default:
Log.e("STATUS", "un-supported forgot password state");
break;
}
}
});
}

        @Override
        public void onError(Exception e) {
            Log.e("STATUS", "forgot password error", e);
        }
    });

```

Yes, i am using non-registered email id. I tested with many dummy email ids as well, but i didnt get any exception. Any specific COGNITO changes to be done at server.

I am facing the same issue! No exception is thrown if the username provided by the user is invalid.

Hmm, I just tried this and got the proper exception. Are you making sure to only call the forgotPassword command after AWSMobileClient initialization succeeds?

Also what state is it in when you make this call (GUEST, SIGNED_OUT, SIGNED_IN, etc.)?

Could you try setting up a basic new project with default settings and see if it still happens for you when you call forgotPassword with a random email?

@TrekSoft
I have the same issue. I confirm that the state is SIGNED_OUT and I call the forgotPssword after successful initialization of AWSMobileClient initialization

@mhassanist - could you post your code snippet?

Also could you post your dependencies from the gradle build file so I can see the versions you are using?

@TrekSoft Sure. Here you go.

Module Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.metasystem.mxpsdk.awscognitosample"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.16.9'
    implementation 'com.amazonaws:aws-android-sdk-auth-userpools:2.16.9'
}

Project Gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

MainActivity Code

package com.metasystem.mxpsdk.awscognitosample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AnonymousAWSCredentials;
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.mobile.client.AWSMobileClient;
import com.amazonaws.mobile.client.Callback;
import com.amazonaws.mobile.client.UserState;
import com.amazonaws.mobile.client.UserStateDetails;
import com.amazonaws.mobile.client.results.ForgotPasswordResult;
import com.amazonaws.mobile.client.results.SignInResult;
import com.amazonaws.mobile.client.results.SignUpResult;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.VerificationHandler;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "aws";

    void init() {
       AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {
            @Override
            public void onResult(UserStateDetails userStateDetails) {
                if (userStateDetails.getUserState() == UserState.SIGNED_IN) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            makeToast("Sign-in done before ");
                            String token="";
                            try {
                                token = AWSMobileClient.getInstance().getTokens().getIdToken().getTokenString();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            makeToast("Sign-in done. Token:" + token);
                        }
                    });

                } else if (userStateDetails.getUserState() == UserState.SIGNED_OUT) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            makeToast("Not signed in will sign in ");
                        }
                    });
                    forgotPass();
                }
            }

            @Override
            public void onError(Exception e) {
                Log.e("INIT", e.toString());
            }
        });
    }


    void forgotPass() {

        AWSMobileClient.getInstance().forgotPassword("[email protected]",
                 new Callback<ForgotPasswordResult>() {
                    @Override
                    public void onResult(final ForgotPasswordResult signInResult) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.d(TAG, "Sign-in callback state: ");


                            }
                        });
                    }

                    @Override
                    public void onError(Exception e) {
                        Log.e(TAG, e.getMessage());
                    }
                });
    }

 private void makeToast(String s) {
        Log.d(TAG,s);
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();


    }
}

Alright thanks, I'll try this out and get back to you later this week.

Hello @TrekSoft
I managed to solve the problem. There is a setting in the Cognito console that was checked by default. I changed it to Legacy and it worked.

in Cognito Console --> App Clients --> Show Details

Prevent User Existence Errors
[ ] Legacy // <-- this one works
[ ] Enabled (Recommended) //was checked by default

Very interesting - thank you for that! As we work on providing a better Auth experience, we'll make sure to address the confusion around that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pedronveloso picture pedronveloso  路  3Comments

yairkukielka picture yairkukielka  路  3Comments

logo17 picture logo17  路  4Comments

devxpy picture devxpy  路  4Comments

shabana0508 picture shabana0508  路  3Comments