use of com.firebase.ui.auth.FirebaseUiException in project FirebaseUI-Android by firebase.
the class SingleSignInActivity method onCreate.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
User user = User.getUser(getIntent());
final String provider = user.getProviderId();
AuthUI.IdpConfig providerConfig = ProviderUtils.getConfigFromIdps(getFlowParams().providers, provider);
if (providerConfig == null) {
finish(RESULT_CANCELED, IdpResponse.getErrorIntent(new FirebaseUiException(ErrorCodes.DEVELOPER_ERROR, "Provider not enabled: " + provider)));
return;
}
ViewModelProvider supplier = new ViewModelProvider(this);
mHandler = supplier.get(SocialProviderResponseHandler.class);
mHandler.init(getFlowParams());
boolean useEmulator = getAuthUI().isUseEmulator();
switch(provider) {
case GoogleAuthProvider.PROVIDER_ID:
if (useEmulator) {
mProvider = supplier.get(GenericIdpSignInHandler.class).initWith(GenericIdpSignInHandler.getGenericGoogleConfig());
} else {
mProvider = supplier.get(GoogleSignInHandler.class).initWith(new GoogleSignInHandler.Params(providerConfig, user.getEmail()));
}
break;
case FacebookAuthProvider.PROVIDER_ID:
if (useEmulator) {
mProvider = supplier.get(GenericIdpSignInHandler.class).initWith(GenericIdpSignInHandler.getGenericFacebookConfig());
} else {
mProvider = supplier.get(FacebookSignInHandler.class).initWith(providerConfig);
}
break;
default:
if (!TextUtils.isEmpty(providerConfig.getParams().getString(GENERIC_OAUTH_PROVIDER_ID))) {
mProvider = supplier.get(GenericIdpSignInHandler.class).initWith(providerConfig);
break;
}
throw new IllegalStateException("Invalid provider id: " + provider);
}
mProvider.getOperation().observe(this, new ResourceObserver<IdpResponse>(this) {
@Override
protected void onSuccess(@NonNull IdpResponse response) {
boolean useSocialHandler = AuthUI.SOCIAL_PROVIDERS.contains(provider) && !getAuthUI().isUseEmulator();
if (useSocialHandler || !response.isSuccessful()) {
mHandler.startSignIn(response);
return;
}
finish(response.isSuccessful() ? RESULT_OK : RESULT_CANCELED, response.toIntent());
}
@Override
protected void onFailure(@NonNull Exception e) {
if (e instanceof FirebaseAuthAnonymousUpgradeException) {
finish(RESULT_CANCELED, new Intent().putExtra(ExtraConstants.IDP_RESPONSE, IdpResponse.from(e)));
return;
}
mHandler.startSignIn(IdpResponse.from(e));
}
});
mHandler.getOperation().observe(this, new ResourceObserver<IdpResponse>(this) {
@Override
protected void onSuccess(@NonNull IdpResponse response) {
startSaveCredentials(mHandler.getCurrentUser(), response, null);
}
@Override
protected void onFailure(@NonNull Exception e) {
if (e instanceof FirebaseAuthAnonymousUpgradeException) {
IdpResponse res = ((FirebaseAuthAnonymousUpgradeException) e).getResponse();
finish(RESULT_CANCELED, new Intent().putExtra(ExtraConstants.IDP_RESPONSE, res));
} else {
finish(RESULT_CANCELED, IdpResponse.getErrorIntent(e));
}
}
});
if (mHandler.getOperation().getValue() == null) {
mProvider.startSignIn(getAuth(), this, provider);
}
}
use of com.firebase.ui.auth.FirebaseUiException in project FirebaseUI-Android by firebase.
the class PhoneActivity method handleError.
private void handleError(@Nullable Exception e) {
TextInputLayout errorView = getErrorView();
if (errorView == null) {
return;
}
if (e instanceof FirebaseAuthAnonymousUpgradeException) {
IdpResponse response = ((FirebaseAuthAnonymousUpgradeException) e).getResponse();
finish(ErrorCodes.ANONYMOUS_UPGRADE_MERGE_CONFLICT, response.toIntent());
} else if (e instanceof FirebaseAuthException) {
FirebaseAuthError error = FirebaseAuthError.fromException((FirebaseAuthException) e);
if (error == FirebaseAuthError.ERROR_USER_DISABLED) {
IdpResponse response = IdpResponse.from(new FirebaseUiException(ErrorCodes.ERROR_USER_DISABLED));
finish(RESULT_CANCELED, response.toIntent());
return;
}
errorView.setError(getErrorMessage(error));
} else if (e != null) {
errorView.setError(getErrorMessage(FirebaseAuthError.ERROR_UNKNOWN));
} else {
errorView.setError(null);
}
}
use of com.firebase.ui.auth.FirebaseUiException in project FirebaseUI-Android by firebase.
the class GenericIdpSignInHandler method handleAnonymousUpgradeFlow.
private void handleAnonymousUpgradeFlow(final FirebaseAuth auth, final HelperActivityBase activity, final OAuthProvider provider, final FlowParameters flowParameters) {
final boolean useEmulator = activity.getAuthUI().isUseEmulator();
auth.getCurrentUser().startActivityForLinkWithProvider(activity, provider).addOnSuccessListener(authResult -> handleSuccess(useEmulator, provider.getProviderId(), authResult.getUser(), (OAuthCredential) authResult.getCredential(), authResult.getAdditionalUserInfo().isNewUser())).addOnFailureListener(e -> {
if (!(e instanceof FirebaseAuthUserCollisionException)) {
setResult(Resource.forFailure(e));
return;
}
FirebaseAuthUserCollisionException collisionException = (FirebaseAuthUserCollisionException) e;
final AuthCredential credential = collisionException.getUpdatedCredential();
final String email = collisionException.getEmail();
// Case 1: Anonymous user trying to link with an existing user
// Case 2: Anonymous user trying to link with a provider keyed
// by an email that already belongs to an existing account
// (linking flow)
ProviderUtils.fetchSortedProviders(auth, flowParameters, email).addOnSuccessListener(providers -> {
if (providers.isEmpty()) {
String errorMessage = "Unable to complete the linkingflow -" + " the user is using " + "unsupported providers.";
setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.DEVELOPER_ERROR, errorMessage)));
return;
}
if (providers.contains(provider.getProviderId())) {
// Case 1
handleMergeFailure(credential);
} else {
// Case 2 - linking flow to be handled by
// SocialProviderResponseHandler
setResult(Resource.forFailure(new FirebaseUiUserCollisionException(ErrorCodes.ERROR_GENERIC_IDP_RECOVERABLE_ERROR, "Recoverable error.", provider.getProviderId(), email, credential)));
}
});
});
}
use of com.firebase.ui.auth.FirebaseUiException in project FirebaseUI-Android by firebase.
the class EmailLinkSignInHandlerTest method testStartSignIn_invalidLink_expectInvalidLinkError.
@Test
@SuppressWarnings("all")
public void testStartSignIn_invalidLink_expectInvalidLinkError() {
mHandler.getOperation().observeForever(mResponseObserver);
when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(false);
mHandler.startSignIn();
verify(mMockAuth).isSignInWithEmailLink(any(String.class));
ArgumentCaptor<Resource<IdpResponse>> captor = ArgumentCaptor.forClass(Resource.class);
InOrder inOrder = inOrder(mResponseObserver);
inOrder.verify(mResponseObserver).onChanged(argThat(ResourceMatchers.<IdpResponse>isLoading()));
inOrder.verify(mResponseObserver).onChanged(captor.capture());
FirebaseUiException exception = (FirebaseUiException) captor.getValue().getException();
assertThat(exception).isNotNull();
assertThat(exception.getErrorCode()).isEqualTo(ErrorCodes.INVALID_EMAIL_LINK_ERROR);
}
use of com.firebase.ui.auth.FirebaseUiException in project FirebaseUI-Android by firebase.
the class EmailLinkSignInHandlerTest method testStartSignIn_differentDeviceLinkWithAnonymousUpgradeEnabled_expectInvalidLinkError.
@Test
@SuppressWarnings("all")
public void testStartSignIn_differentDeviceLinkWithAnonymousUpgradeEnabled_expectInvalidLinkError() {
String differentSessionId = SessionUtils.generateRandomAlphaNumericString(10);
String anonUserId = SessionUtils.generateRandomAlphaNumericString(10);
initializeHandlerWithSessionInfo(differentSessionId, anonUserId, null, false);
mHandler.getOperation().observeForever(mResponseObserver);
when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
mHandler.startSignIn();
verify(mMockAuth).isSignInWithEmailLink(any(String.class));
ArgumentCaptor<Resource<IdpResponse>> captor = ArgumentCaptor.forClass(Resource.class);
InOrder inOrder = inOrder(mResponseObserver);
inOrder.verify(mResponseObserver).onChanged(argThat(ResourceMatchers.<IdpResponse>isLoading()));
inOrder.verify(mResponseObserver).onChanged(captor.capture());
FirebaseUiException exception = (FirebaseUiException) captor.getValue().getException();
assertThat(exception).isNotNull();
assertThat(exception.getErrorCode()).isEqualTo(ErrorCodes.EMAIL_LINK_WRONG_DEVICE_ERROR);
}
Aggregations