use of com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignInOptions in project amplify-android by aws-amplify.
the class AuthComponentTest method confirmSignInWithOptions.
/**
* Tests that the confirmSignIn method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.confirmSignIn with
* the confirmation code and options it received and the success callback receives a valid AuthSignInResult.
* @throws AuthException test fails if this gets thrown since method should succeed
*/
@Test
// Casts final parameter to Callback to differentiate methods
@SuppressWarnings("unchecked")
public void confirmSignInWithOptions() throws AuthException {
SignInResult amcResult = new SignInResult(SignInState.DONE, new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME));
Tokens tokensResult = new Tokens(ACCESS_TOKEN, ID_TOKEN, REFRESH_TOKEN);
AWSCognitoAuthConfirmSignInOptions.CognitoBuilder options = AWSCognitoAuthConfirmSignInOptions.builder();
Map<String, String> metadata = new HashMap<String, String>();
AuthUserAttribute attribute = new AuthUserAttribute(AuthUserAttributeKey.email(), "test@test.test");
List<AuthUserAttribute> attributes = Collections.singletonList(attribute);
Map<String, String> attributeMap = Collections.singletonMap("email", "test@test.test");
metadata.put("key", "value");
options.metadata(metadata);
options.userAttributes(attributes);
AWSCognitoAuthConfirmSignInOptions builtOptions = options.build();
doAnswer(invocation -> {
Callback<Tokens> callback = invocation.getArgument(0);
callback.onResult(tokensResult);
return null;
}).when(mobileClient).getTokens(any());
doAnswer(invocation -> {
Callback<SignInResult> callback = invocation.getArgument(3);
callback.onResult(amcResult);
return null;
}).when(mobileClient).confirmSignIn(any(String.class), any(), any(), (Callback<SignInResult>) any());
AuthSignInResult result = synchronousAuth.confirmSignIn(CONFIRMATION_CODE, builtOptions);
validateSignInResult(result, true, AuthSignInStep.DONE);
verify(mobileClient).confirmSignIn(eq(CONFIRMATION_CODE), eq(metadata), eq(attributeMap), (Callback<SignInResult>) any());
}
use of com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignInOptions in project amplify-android by aws-amplify.
the class AWSCognitoAuthPlugin method confirmSignIn.
@Override
public void confirmSignIn(@NonNull String confirmationCode, @NonNull AuthConfirmSignInOptions options, @NonNull Consumer<AuthSignInResult> onSuccess, @NonNull Consumer<AuthException> onException) {
final Map<String, String> metadata = new HashMap<>();
final Map<String, String> userAttributes = new HashMap<>();
if (options instanceof AWSCognitoAuthConfirmSignInOptions) {
metadata.putAll(((AWSCognitoAuthConfirmSignInOptions) options).getMetadata());
for (AuthUserAttribute attribute : ((AWSCognitoAuthConfirmSignInOptions) options).getUserAttributes()) {
userAttributes.put(attribute.getKey().getKeyString(), attribute.getValue());
}
}
awsMobileClient.confirmSignIn(confirmationCode, metadata, userAttributes, new Callback<SignInResult>() {
@Override
public void onResult(SignInResult result) {
try {
AuthSignInResult newResult = convertSignInResult(result);
fetchAndSetUserId(() -> onSuccess.accept(newResult));
} catch (AuthException exception) {
onException.accept(exception);
}
}
@Override
public void onError(Exception error) {
onException.accept(CognitoAuthExceptionConverter.lookup(error, "Confirm sign in failed"));
}
});
}
Aggregations