Search in sources :

Example 1 with AuthNextResetPasswordStep

use of com.amplifyframework.auth.result.step.AuthNextResetPasswordStep in project amplify-android by aws-amplify.

the class AWSCognitoAuthPlugin method resetPassword.

@Override
public void resetPassword(@NonNull String username, @NonNull AuthResetPasswordOptions options, @NonNull Consumer<AuthResetPasswordResult> onSuccess, @NonNull Consumer<AuthException> onException) {
    final Map<String, String> clientMetadata = new HashMap<>();
    if (options instanceof AWSCognitoAuthResetPasswordOptions) {
        AWSCognitoAuthResetPasswordOptions cognitoOptions = (AWSCognitoAuthResetPasswordOptions) options;
        clientMetadata.putAll(cognitoOptions.getMetadata());
    }
    awsMobileClient.forgotPassword(username, clientMetadata, new Callback<ForgotPasswordResult>() {

        @Override
        public void onResult(ForgotPasswordResult result) {
            if (result.getState().equals(ForgotPasswordState.CONFIRMATION_CODE)) {
                onSuccess.accept(new AuthResetPasswordResult(false, new AuthNextResetPasswordStep(AuthResetPasswordStep.CONFIRM_RESET_PASSWORD_WITH_CODE, Collections.emptyMap(), convertCodeDeliveryDetails(result.getParameters()))));
            } else {
                onException.accept(new AuthException("Received an unsupported response after triggering password recovery: " + result.getState(), "This is almost certainly a bug. Please report it as an issue in our GitHub repo."));
            }
        }

        @Override
        public void onError(Exception exception) {
            onException.accept(CognitoAuthExceptionConverter.lookup(exception, "Reset password failed."));
        }
    });
}
Also used : HashMap(java.util.HashMap) AuthException(com.amplifyframework.auth.AuthException) AuthNextResetPasswordStep(com.amplifyframework.auth.result.step.AuthNextResetPasswordStep) ForgotPasswordResult(com.amazonaws.mobile.client.results.ForgotPasswordResult) AWSCognitoAuthResetPasswordOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthResetPasswordOptions) AuthResetPasswordResult(com.amplifyframework.auth.result.AuthResetPasswordResult) 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 AuthNextResetPasswordStep

use of com.amplifyframework.auth.result.step.AuthNextResetPasswordStep in project amplify-android by aws-amplify.

the class RxAuthBindingTest method testResetPasswordSucceeds.

/**
 * Tests that a successful request to reset the password will propagate a result
 * back through the binding.
 * @throws InterruptedException If test observer is interrupted while awaiting terminal event
 */
@Test
public void testResetPasswordSucceeds() throws InterruptedException {
    String username = RandomString.string();
    // Arrange delegate to furnish a result
    AuthResetPasswordStep step = AuthResetPasswordStep.CONFIRM_RESET_PASSWORD_WITH_CODE;
    AuthCodeDeliveryDetails details = new AuthCodeDeliveryDetails(RandomString.string(), DeliveryMedium.PHONE);
    AuthNextResetPasswordStep nextStep = new AuthNextResetPasswordStep(step, Collections.emptyMap(), details);
    AuthResetPasswordResult expected = new AuthResetPasswordResult(true, nextStep);
    doAnswer(invocation -> {
        // 0 = username, 1 = onResult, 2 = onFailure
        int positionOfResultConsumer = 1;
        Consumer<AuthResetPasswordResult> onResult = invocation.getArgument(positionOfResultConsumer);
        onResult.accept(expected);
        return null;
    }).when(delegate).resetPassword(eq(username), anyConsumer(), anyConsumer());
    // Act: call the binding
    TestObserver<AuthResetPasswordResult> observer = auth.resetPassword(username).test();
    // Assert: result was furnished via Rx Single
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertNoErrors().assertValue(expected);
}
Also used : AuthCodeDeliveryDetails(com.amplifyframework.auth.AuthCodeDeliveryDetails) AuthNextResetPasswordStep(com.amplifyframework.auth.result.step.AuthNextResetPasswordStep) RandomString(com.amplifyframework.testutils.random.RandomString) AuthResetPasswordResult(com.amplifyframework.auth.result.AuthResetPasswordResult) AuthResetPasswordStep(com.amplifyframework.auth.result.step.AuthResetPasswordStep) Test(org.junit.Test)

Aggregations

AuthResetPasswordResult (com.amplifyframework.auth.result.AuthResetPasswordResult)2 AuthNextResetPasswordStep (com.amplifyframework.auth.result.step.AuthNextResetPasswordStep)2 ForgotPasswordResult (com.amazonaws.mobile.client.results.ForgotPasswordResult)1 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 AWSCognitoAuthResetPasswordOptions (com.amplifyframework.auth.cognito.options.AWSCognitoAuthResetPasswordOptions)1 AuthResetPasswordStep (com.amplifyframework.auth.result.step.AuthResetPasswordStep)1 RandomString (com.amplifyframework.testutils.random.RandomString)1 HashMap (java.util.HashMap)1 JSONException (org.json.JSONException)1 Test (org.junit.Test)1