Search in sources :

Example 1 with FirebaseUiException

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);
    }
}
Also used : User(com.firebase.ui.auth.data.model.User) Intent(android.content.Intent) AuthUI(com.firebase.ui.auth.AuthUI) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) SocialProviderResponseHandler(com.firebase.ui.auth.viewmodel.idp.SocialProviderResponseHandler) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) ViewModelProvider(androidx.lifecycle.ViewModelProvider) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 2 with FirebaseUiException

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);
    }
}
Also used : FirebaseAuthError(com.firebase.ui.auth.util.FirebaseAuthError) TextInputLayout(com.google.android.material.textfield.TextInputLayout) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 3 with FirebaseUiException

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)));
            }
        });
    });
}
Also used : ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) NonNull(androidx.annotation.NonNull) User(com.firebase.ui.auth.data.model.User) Intent(android.content.Intent) Resource(com.firebase.ui.auth.data.model.Resource) ExtraConstants(com.firebase.ui.auth.util.ExtraConstants) HashMap(java.util.HashMap) FirebaseUiUserCollisionException(com.firebase.ui.auth.FirebaseUiUserCollisionException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) GoogleAuthProvider(com.google.firebase.auth.GoogleAuthProvider) OAuthCredential(com.google.firebase.auth.OAuthCredential) RestrictTo(androidx.annotation.RestrictTo) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) AuthUI(com.firebase.ui.auth.AuthUI) R(com.firebase.ui.auth.R) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) UserCancellationException(com.firebase.ui.auth.data.model.UserCancellationException) HelperActivityBase(com.firebase.ui.auth.ui.HelperActivityBase) FirebaseUser(com.google.firebase.auth.FirebaseUser) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) FacebookAuthProvider(com.google.firebase.auth.FacebookAuthProvider) FirebaseAuthError(com.firebase.ui.auth.util.FirebaseAuthError) OAuthProvider(com.google.firebase.auth.OAuthProvider) AuthCredential(com.google.firebase.auth.AuthCredential) FlowParameters(com.firebase.ui.auth.data.model.FlowParameters) List(java.util.List) Nullable(androidx.annotation.Nullable) Application(android.app.Application) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException) ProviderSignInBase(com.firebase.ui.auth.viewmodel.ProviderSignInBase) RequestCodes(com.firebase.ui.auth.viewmodel.RequestCodes) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) VisibleForTesting(androidx.annotation.VisibleForTesting) IdpResponse(com.firebase.ui.auth.IdpResponse) OAuthCredential(com.google.firebase.auth.OAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseUiUserCollisionException(com.firebase.ui.auth.FirebaseUiUserCollisionException)

Example 4 with FirebaseUiException

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);
}
Also used : InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 5 with FirebaseUiException

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);
}
Also used : InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Aggregations

FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)22 IdpResponse (com.firebase.ui.auth.IdpResponse)16 Resource (com.firebase.ui.auth.data.model.Resource)9 FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)8 ViewModelProvider (androidx.lifecycle.ViewModelProvider)6 Test (org.junit.Test)6 InOrder (org.mockito.InOrder)6 User (com.firebase.ui.auth.data.model.User)5 Intent (android.content.Intent)4 AuthUI (com.firebase.ui.auth.AuthUI)4 UserCancellationException (com.firebase.ui.auth.data.model.UserCancellationException)4 FirebaseAuthError (com.firebase.ui.auth.util.FirebaseAuthError)4 AuthOperationManager (com.firebase.ui.auth.util.data.AuthOperationManager)4 FirebaseAuthException (com.google.firebase.auth.FirebaseAuthException)4 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)4 Application (android.app.Application)3 TextView (android.widget.TextView)3 NonNull (androidx.annotation.NonNull)3 Nullable (androidx.annotation.Nullable)3 RestrictTo (androidx.annotation.RestrictTo)3