Search in sources :

Example 1 with AWSCognitoAuthResendUserAttributeConfirmationCodeOptions

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

the class AWSCognitoAuthPlugin method resendUserAttributeConfirmationCode.

@Override
public void resendUserAttributeConfirmationCode(@NonNull AuthUserAttributeKey attributeKey, @NonNull AuthResendUserAttributeConfirmationCodeOptions options, @NonNull Consumer<AuthCodeDeliveryDetails> onSuccess, @NonNull Consumer<AuthException> onError) {
    final Map<String, String> clientMetadata = new HashMap<>();
    if (options instanceof AWSCognitoAuthResendUserAttributeConfirmationCodeOptions) {
        AWSCognitoAuthResendUserAttributeConfirmationCodeOptions cognitoOptions = (AWSCognitoAuthResendUserAttributeConfirmationCodeOptions) options;
        clientMetadata.putAll(cognitoOptions.getMetadata());
    }
    String attributeName = attributeKey.getKeyString();
    awsMobileClient.verifyUserAttribute(attributeName, clientMetadata, new Callback<UserCodeDeliveryDetails>() {

        @Override
        public void onResult(UserCodeDeliveryDetails result) {
            onSuccess.accept(convertCodeDeliveryDetails(result));
        }

        @Override
        public void onError(Exception error) {
            onError.accept(new AuthException("Failed to resend user attribute confirmation code", error, "See attached exception for more details"));
        }
    });
}
Also used : HashMap(java.util.HashMap) AWSCognitoAuthResendUserAttributeConfirmationCodeOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthResendUserAttributeConfirmationCodeOptions) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthException(com.amplifyframework.auth.AuthException) 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 AWSCognitoAuthResendUserAttributeConfirmationCodeOptions

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

the class AuthComponentTest method resendUserAttributeConfirmationCodeWithOptions.

/**
 * Tests the resendUserAttributeConfirmationCode method of the Auth wrapper of AWSMobileClient (AMC) Calls
 * AMC.verifyUserAttribute with the user attribute key to be verified and options it received.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void resendUserAttributeConfirmationCodeWithOptions() throws AuthException {
    AuthUserAttributeKey attributeKey = AuthUserAttributeKey.custom(ATTRIBUTE_KEY);
    UserCodeDeliveryDetails userCodeDeliveryDetails = new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME);
    AWSCognitoAuthResendUserAttributeConfirmationCodeOptions.CognitoBuilder options = AWSCognitoAuthResendUserAttributeConfirmationCodeOptions.builder();
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("key", "value");
    options.metadata(metadata);
    AWSCognitoAuthResendUserAttributeConfirmationCodeOptions builtOptions = options.build();
    doAnswer(invocation -> {
        Callback<UserCodeDeliveryDetails> callback = invocation.getArgument(2);
        callback.onResult(userCodeDeliveryDetails);
        return null;
    }).when(mobileClient).verifyUserAttribute(any(), any(), Mockito.<Callback<UserCodeDeliveryDetails>>any());
    AuthCodeDeliveryDetails result = synchronousAuth.resendUserAttributeConfirmationCode(attributeKey, builtOptions);
    validateCodeDeliveryDetails(result);
    verify(mobileClient).verifyUserAttribute(eq(attributeKey.getKeyString()), eq(metadata), Mockito.<Callback<UserCodeDeliveryDetails>>any());
}
Also used : AuthCodeDeliveryDetails(com.amplifyframework.auth.AuthCodeDeliveryDetails) AWSCognitoAuthResendUserAttributeConfirmationCodeOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthResendUserAttributeConfirmationCodeOptions) HashMap(java.util.HashMap) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthUserAttributeKey(com.amplifyframework.auth.AuthUserAttributeKey) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)2 AWSCognitoAuthResendUserAttributeConfirmationCodeOptions (com.amplifyframework.auth.cognito.options.AWSCognitoAuthResendUserAttributeConfirmationCodeOptions)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 AuthCodeDeliveryDetails (com.amplifyframework.auth.AuthCodeDeliveryDetails)1 AuthException (com.amplifyframework.auth.AuthException)1 AuthUserAttributeKey (com.amplifyframework.auth.AuthUserAttributeKey)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