Search in sources :

Example 11 with AuthUserAttribute

use of com.amplifyframework.auth.AuthUserAttribute in project amplify-android by aws-amplify.

the class AWSCognitoAuthPlugin method updateUserAttributes.

@Override
public void updateUserAttributes(@NonNull List<AuthUserAttribute> attributes, @NonNull AuthUpdateUserAttributesOptions options, @NonNull Consumer<Map<AuthUserAttributeKey, AuthUpdateAttributeResult>> onSuccess, @NonNull Consumer<AuthException> onError) {
    final Map<String, String> clientMetadata = new HashMap<>();
    if (options instanceof AWSCognitoAuthUpdateUserAttributesOptions) {
        AWSCognitoAuthUpdateUserAttributesOptions cognitoOptions = (AWSCognitoAuthUpdateUserAttributesOptions) options;
        clientMetadata.putAll(cognitoOptions.getMetadata());
    }
    Map<String, String> attributesMap = new HashMap<>();
    for (AuthUserAttribute attribute : attributes) {
        attributesMap.put(attribute.getKey().getKeyString(), attribute.getValue());
    }
    awsMobileClient.updateUserAttributes(attributesMap, clientMetadata, new Callback<List<UserCodeDeliveryDetails>>() {

        @Override
        public void onResult(List<UserCodeDeliveryDetails> result) {
            Map<String, UserCodeDeliveryDetails> codeDetailsMap = new HashMap<>();
            Map<AuthUserAttributeKey, AuthUpdateAttributeResult> resultMap = new HashMap<>();
            for (UserCodeDeliveryDetails details : result) {
                codeDetailsMap.put(details.getAttributeName(), details);
            }
            for (String attributeKey : attributesMap.keySet()) {
                if (codeDetailsMap.containsKey(attributeKey)) {
                    resultMap.put(AuthUserAttributeKey.custom(attributeKey), new AuthUpdateAttributeResult(true, new AuthNextUpdateAttributeStep(AuthUpdateAttributeStep.CONFIRM_ATTRIBUTE_WITH_CODE, Collections.emptyMap(), convertCodeDeliveryDetails(codeDetailsMap.get(attributeKey)))));
                } else {
                    resultMap.put(AuthUserAttributeKey.custom(attributeKey), new AuthUpdateAttributeResult(true, new AuthNextUpdateAttributeStep(AuthUpdateAttributeStep.DONE, Collections.emptyMap(), null)));
                }
            }
            onSuccess.accept(resultMap);
        }

        @Override
        public void onError(Exception error) {
            onError.accept(new AuthException("Failed to update user attributes", error, "See attached exception for more details"));
        }
    });
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) AuthUpdateAttributeResult(com.amplifyframework.auth.result.AuthUpdateAttributeResult) AuthNextUpdateAttributeStep(com.amplifyframework.auth.result.step.AuthNextUpdateAttributeStep) HashMap(java.util.HashMap) 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) AWSCognitoAuthUpdateUserAttributesOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributesOptions) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

AuthUserAttribute (com.amplifyframework.auth.AuthUserAttribute)11 HashMap (java.util.HashMap)8 Test (org.junit.Test)8 AuthUpdateAttributeResult (com.amplifyframework.auth.result.AuthUpdateAttributeResult)7 ArrayList (java.util.ArrayList)7 UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)6 List (java.util.List)6 RandomString (com.amplifyframework.testutils.random.RandomString)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 AuthNavigationException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException)3 NotAuthorizedException (com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException)3 AmplifyException (com.amplifyframework.AmplifyException)3 AuthException (com.amplifyframework.auth.AuthException)3 AuthUserAttributeKey (com.amplifyframework.auth.AuthUserAttributeKey)3 AuthNextUpdateAttributeStep (com.amplifyframework.auth.result.step.AuthNextUpdateAttributeStep)3 JSONException (org.json.JSONException)3 SignInResult (com.amazonaws.mobile.client.results.SignInResult)2 AWSCognitoAuthConfirmSignInOptions (com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignInOptions)2 AWSCognitoAuthUpdateUserAttributesOptions (com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributesOptions)2 AuthSignInResult (com.amplifyframework.auth.result.AuthSignInResult)2