Search in sources :

Example 1 with AWSCognitoAuthConfirmResetPasswordOptions

use of com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmResetPasswordOptions in project amplify-android by aws-amplify.

the class AWSCognitoAuthPlugin method confirmResetPassword.

@Override
public void confirmResetPassword(@NonNull String newPassword, @NonNull String confirmationCode, @NonNull AuthConfirmResetPasswordOptions options, @NonNull Action onSuccess, @NonNull Consumer<AuthException> onException) {
    final Map<String, String> clientMetadata = new HashMap<>();
    if (options instanceof AWSCognitoAuthConfirmResetPasswordOptions) {
        AWSCognitoAuthConfirmResetPasswordOptions cognitoOptions = (AWSCognitoAuthConfirmResetPasswordOptions) options;
        clientMetadata.putAll(cognitoOptions.getMetadata());
    }
    awsMobileClient.confirmForgotPassword(newPassword, confirmationCode, clientMetadata, new Callback<ForgotPasswordResult>() {

        @Override
        public void onResult(ForgotPasswordResult result) {
            if (result.getState().equals(ForgotPasswordState.DONE)) {
                onSuccess.call();
            } else {
                onException.accept(new AuthException("Received an unsupported response while confirming password recovery code: " + result.getState(), "This is almost certainly a bug. Please report it as an issue in our GitHub repo."));
            }
        }

        @Override
        public void onError(Exception error) {
            onException.accept(CognitoAuthExceptionConverter.lookup(error, "Confirm reset password failed."));
        }
    });
}
Also used : AWSCognitoAuthConfirmResetPasswordOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmResetPasswordOptions) HashMap(java.util.HashMap) AuthException(com.amplifyframework.auth.AuthException) ForgotPasswordResult(com.amazonaws.mobile.client.results.ForgotPasswordResult) AuthException(com.amplifyframework.auth.AuthException) JSONException(org.json.JSONException) AmplifyException(com.amplifyframework.AmplifyException) NotAuthorizedException(com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException) AuthNavigationException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException)

Example 2 with AWSCognitoAuthConfirmResetPasswordOptions

use of com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmResetPasswordOptions in project amplify-android by aws-amplify.

the class AuthComponentTest method confirmResetPasswordWithOptions.

/**
 * Tests that the confirmResetPassword method of the Auth wrapper of AWSMobileClient (AMC) calls
 * AMC.confirmForgotPassword with the new password and confirmation code it received.
 * Also ensures that in the onResult case, the success callback is triggered and in the onError case,
 * the error call back receives an AuthException with the root cause attached.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void confirmResetPasswordWithOptions() throws AuthException {
    ForgotPasswordResult amcResult = new ForgotPasswordResult(ForgotPasswordState.DONE);
    AWSCognitoAuthConfirmResetPasswordOptions.CognitoBuilder options = AWSCognitoAuthConfirmResetPasswordOptions.builder();
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("key", "value");
    options.metadata(metadata);
    AWSCognitoAuthConfirmResetPasswordOptions builtOptions = options.build();
    doAnswer(invocation -> {
        Callback<ForgotPasswordResult> callback = invocation.getArgument(3);
        callback.onResult(amcResult);
        return null;
    }).when(mobileClient).confirmForgotPassword(anyString(), anyString(), anyMap(), Mockito.any());
    synchronousAuth.confirmResetPassword(NEW_PASSWORD, CONFIRMATION_CODE, builtOptions);
    verify(mobileClient).confirmForgotPassword(eq(NEW_PASSWORD), eq(CONFIRMATION_CODE), eq(metadata), any());
}
Also used : AWSCognitoAuthConfirmResetPasswordOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmResetPasswordOptions) HashMap(java.util.HashMap) ForgotPasswordResult(com.amazonaws.mobile.client.results.ForgotPasswordResult) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

ForgotPasswordResult (com.amazonaws.mobile.client.results.ForgotPasswordResult)2 AWSCognitoAuthConfirmResetPasswordOptions (com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmResetPasswordOptions)2 HashMap (java.util.HashMap)2 AuthNavigationException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException)1 NotAuthorizedException (com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException)1 AmplifyException (com.amplifyframework.AmplifyException)1 AuthException (com.amplifyframework.auth.AuthException)1 RandomString (com.amplifyframework.testutils.random.RandomString)1 JSONException (org.json.JSONException)1 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1