Search in sources :

Example 11 with FirebaseAuthAnonymousUpgradeException

use of com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException in project FirebaseUI-Android by firebase.

the class PhoneProviderResponseHandlerTest method testSignIn_autoUpgradeAnonymousEnabledWithExistingUser_expectMergeFailure.

@Test
public void testSignIn_autoUpgradeAnonymousEnabledWithExistingUser_expectMergeFailure() {
    mHandler.getOperation().observeForever(mResponseObserver);
    setupAnonymousUpgrade();
    FirebaseAuthUserCollisionException ex = new FirebaseAuthUserCollisionException("foo", "bar");
    TestHelper.setPrivateField(ex, FirebaseAuthUserCollisionException.class, AuthCredential.class, mCredential);
    when(mMockAuth.getCurrentUser().linkWithCredential(mCredential)).thenReturn(AutoCompleteTask.forFailure(ex));
    IdpResponse response = new IdpResponse.Builder(new User.Builder(PhoneAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build()).build();
    mHandler.startSignIn(mCredential, response);
    verify(mMockAuth.getCurrentUser()).linkWithCredential(mCredential);
    InOrder inOrder = inOrder(mResponseObserver);
    inOrder.verify(mResponseObserver).onChanged(argThat(ResourceMatchers.isLoading()));
    ArgumentCaptor<Resource<IdpResponse>> resolveCaptor = ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResponseObserver).onChanged(resolveCaptor.capture());
    FirebaseAuthAnonymousUpgradeException e = (FirebaseAuthAnonymousUpgradeException) resolveCaptor.getValue().getException();
    assertThat(e.getResponse().getCredentialForLinking()).isNotNull();
}
Also used : User(com.firebase.ui.auth.data.model.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 12 with FirebaseAuthAnonymousUpgradeException

use of com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException in project FirebaseUI-Android by firebase.

the class WelcomeBackPasswordPrompt method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fui_welcome_back_password_prompt_layout);
    // Show keyboard
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    mIdpResponse = IdpResponse.fromResultIntent(getIntent());
    String email = mIdpResponse.getEmail();
    mDoneButton = findViewById(R.id.button_done);
    mProgressBar = findViewById(R.id.top_progress_bar);
    mPasswordLayout = findViewById(R.id.password_layout);
    mPasswordField = findViewById(R.id.password);
    ImeHelper.setImeOnDoneListener(mPasswordField, this);
    // Create welcome back text with email bolded.
    String bodyText = getString(R.string.fui_welcome_back_password_prompt_body, email);
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(bodyText);
    TextHelper.boldAllOccurencesOfText(spannableStringBuilder, bodyText, email);
    TextView bodyTextView = findViewById(R.id.welcome_back_password_body);
    bodyTextView.setText(spannableStringBuilder);
    // Click listeners
    mDoneButton.setOnClickListener(this);
    findViewById(R.id.trouble_signing_in).setOnClickListener(this);
    // Initialize ViewModel with arguments
    mHandler = new ViewModelProvider(this).get(WelcomeBackPasswordHandler.class);
    mHandler.init(getFlowParams());
    // Observe the state of the main auth operation
    mHandler.getOperation().observe(this, new ResourceObserver<IdpResponse>(this, R.string.fui_progress_dialog_signing_in) {

        @Override
        protected void onSuccess(@NonNull IdpResponse response) {
            startSaveCredentials(mHandler.getCurrentUser(), response, mHandler.getPendingPassword());
        }

        @Override
        protected void onFailure(@NonNull Exception e) {
            if (e instanceof FirebaseAuthAnonymousUpgradeException) {
                IdpResponse response = ((FirebaseAuthAnonymousUpgradeException) e).getResponse();
                finish(ErrorCodes.ANONYMOUS_UPGRADE_MERGE_CONFLICT, response.toIntent());
                return;
            }
            if (e instanceof FirebaseAuthException) {
                FirebaseAuthException authEx = (FirebaseAuthException) e;
                FirebaseAuthError error = FirebaseAuthError.fromException(authEx);
                if (error == FirebaseAuthError.ERROR_USER_DISABLED) {
                    IdpResponse resp = IdpResponse.from(new FirebaseUiException(ErrorCodes.ERROR_USER_DISABLED));
                    finish(RESULT_CANCELED, resp.toIntent());
                    return;
                }
            }
            mPasswordLayout.setError(getString(getErrorMessage(e)));
        }
    });
    TextView footerText = findViewById(R.id.email_footer_tos_and_pp_text);
    PrivacyDisclosureUtils.setupTermsOfServiceFooter(this, getFlowParams(), footerText);
}
Also used : FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) FirebaseAuthInvalidCredentialsException(com.google.firebase.auth.FirebaseAuthInvalidCredentialsException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) WelcomeBackPasswordHandler(com.firebase.ui.auth.viewmodel.email.WelcomeBackPasswordHandler) FirebaseAuthError(com.firebase.ui.auth.util.FirebaseAuthError) TextView(android.widget.TextView) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) SpannableStringBuilder(android.text.SpannableStringBuilder) ViewModelProvider(androidx.lifecycle.ViewModelProvider) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 13 with FirebaseAuthAnonymousUpgradeException

use of com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException in project FirebaseUI-Android by firebase.

the class AuthMethodPickerActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FlowParameters params = getFlowParams();
    customLayout = params.authMethodPickerLayout;
    mHandler = new ViewModelProvider(this).get(SocialProviderResponseHandler.class);
    mHandler.init(params);
    mProviders = new ArrayList<>();
    if (customLayout != null) {
        setContentView(customLayout.getMainLayout());
        // Setup using custom layout
        populateIdpListCustomLayout(params.providers);
    } else {
        setContentView(R.layout.fui_auth_method_picker_layout);
        // UI only with default layout
        mProgressBar = findViewById(R.id.top_progress_bar);
        mProviderHolder = findViewById(R.id.btn_holder);
        populateIdpList(params.providers);
        int logoId = params.logoId;
        if (logoId == AuthUI.NO_LOGO) {
            findViewById(R.id.logo).setVisibility(View.GONE);
            ConstraintLayout layout = findViewById(R.id.root);
            ConstraintSet constraints = new ConstraintSet();
            constraints.clone(layout);
            constraints.setHorizontalBias(R.id.container, 0.5f);
            constraints.setVerticalBias(R.id.container, 0.5f);
            constraints.applyTo(layout);
        } else {
            ImageView logo = findViewById(R.id.logo);
            logo.setImageResource(logoId);
        }
    }
    boolean tosAndPpConfigured = getFlowParams().isPrivacyPolicyUrlProvided() && getFlowParams().isTermsOfServiceUrlProvided();
    int termsTextId = customLayout == null ? R.id.main_tos_and_pp : customLayout.getTosPpView();
    if (termsTextId >= 0) {
        TextView termsText = findViewById(termsTextId);
        // No ToS or PP provided, so we should hide the view entirely
        if (!tosAndPpConfigured) {
            termsText.setVisibility(View.GONE);
        } else {
            PrivacyDisclosureUtils.setupTermsOfServiceAndPrivacyPolicyText(this, getFlowParams(), termsText);
        }
    }
    // Handler for both
    mHandler.getOperation().observe(this, new ResourceObserver<IdpResponse>(this, R.string.fui_progress_dialog_signing_in) {

        @Override
        protected void onSuccess(@NonNull IdpResponse response) {
            startSaveCredentials(mHandler.getCurrentUser(), response, null);
        }

        @Override
        protected void onFailure(@NonNull Exception e) {
            if (e instanceof UserCancellationException) {
                // User pressed back, there is no error.
                return;
            }
            if (e instanceof FirebaseAuthAnonymousUpgradeException) {
                finish(ErrorCodes.ANONYMOUS_UPGRADE_MERGE_CONFLICT, ((FirebaseAuthAnonymousUpgradeException) e).getResponse().toIntent());
            } else if (e instanceof FirebaseUiException) {
                FirebaseUiException fue = (FirebaseUiException) e;
                finish(RESULT_CANCELED, IdpResponse.from(fue).toIntent());
            } else {
                String text = getString(R.string.fui_error_unknown);
                Toast.makeText(AuthMethodPickerActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        }
    });
}
Also used : ConstraintSet(androidx.constraintlayout.widget.ConstraintSet) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) UserCancellationException(com.firebase.ui.auth.data.model.UserCancellationException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) ConstraintLayout(androidx.constraintlayout.widget.ConstraintLayout) FlowParameters(com.firebase.ui.auth.data.model.FlowParameters) SocialProviderResponseHandler(com.firebase.ui.auth.viewmodel.idp.SocialProviderResponseHandler) UserCancellationException(com.firebase.ui.auth.data.model.UserCancellationException) TextView(android.widget.TextView) ImageView(android.widget.ImageView) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) ViewModelProvider(androidx.lifecycle.ViewModelProvider) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 14 with FirebaseAuthAnonymousUpgradeException

use of com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException in project FirebaseUI-Android by firebase.

the class WelcomeBackIdpPrompt method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fui_welcome_back_idp_prompt_layout);
    mDoneButton = findViewById(R.id.welcome_back_idp_button);
    mProgressBar = findViewById(R.id.top_progress_bar);
    mPromptText = findViewById(R.id.welcome_back_idp_prompt);
    User existingUser = User.getUser(getIntent());
    IdpResponse requestedUserResponse = IdpResponse.fromResultIntent(getIntent());
    ViewModelProvider supplier = new ViewModelProvider(this);
    final LinkingSocialProviderResponseHandler handler = supplier.get(LinkingSocialProviderResponseHandler.class);
    handler.init(getFlowParams());
    if (requestedUserResponse != null) {
        handler.setRequestedSignInCredentialForEmail(ProviderUtils.getAuthCredential(requestedUserResponse), existingUser.getEmail());
    }
    final String providerId = existingUser.getProviderId();
    AuthUI.IdpConfig config = ProviderUtils.getConfigFromIdps(getFlowParams().providers, providerId);
    if (config == null) {
        finish(RESULT_CANCELED, IdpResponse.getErrorIntent(new FirebaseUiException(ErrorCodes.DEVELOPER_ERROR, "Firebase login unsuccessful." + " Account linking failed due to provider not enabled by application: " + providerId)));
        return;
    }
    String providerName;
    String genericOAuthProviderId = config.getParams().getString(ExtraConstants.GENERIC_OAUTH_PROVIDER_ID);
    boolean useEmulator = getAuthUI().isUseEmulator();
    switch(providerId) {
        case GoogleAuthProvider.PROVIDER_ID:
            if (useEmulator) {
                mProvider = supplier.get(GenericIdpAnonymousUpgradeLinkingHandler.class).initWith(GenericIdpSignInHandler.getGenericGoogleConfig());
            } else {
                mProvider = supplier.get(GoogleSignInHandler.class).initWith(new GoogleSignInHandler.Params(config, existingUser.getEmail()));
            }
            providerName = getString(R.string.fui_idp_name_google);
            break;
        case FacebookAuthProvider.PROVIDER_ID:
            if (useEmulator) {
                mProvider = supplier.get(GenericIdpAnonymousUpgradeLinkingHandler.class).initWith(GenericIdpSignInHandler.getGenericFacebookConfig());
            } else {
                mProvider = supplier.get(FacebookSignInHandler.class).initWith(config);
            }
            providerName = getString(R.string.fui_idp_name_facebook);
            break;
        default:
            if (TextUtils.equals(providerId, genericOAuthProviderId)) {
                mProvider = supplier.get(GenericIdpAnonymousUpgradeLinkingHandler.class).initWith(config);
                providerName = config.getParams().getString(ExtraConstants.GENERIC_OAUTH_PROVIDER_NAME);
            } else {
                throw new IllegalStateException("Invalid provider id: " + providerId);
            }
    }
    mProvider.getOperation().observe(this, new ResourceObserver<IdpResponse>(this) {

        @Override
        protected void onSuccess(@NonNull IdpResponse response) {
            boolean isGenericIdp = getAuthUI().isUseEmulator() || !AuthUI.SOCIAL_PROVIDERS.contains(response.getProviderType());
            if (isGenericIdp && !response.hasCredentialForLinking() && !handler.hasCredentialForLinking()) {
                // Generic Idp does not return a credential - if this is not a linking flow,
                // the user is already signed in and we are done.
                finish(RESULT_OK, response.toIntent());
                return;
            }
            handler.startSignIn(response);
        }

        @Override
        protected void onFailure(@NonNull Exception e) {
            handler.startSignIn(IdpResponse.from(e));
        }
    });
    mPromptText.setText(getString(R.string.fui_welcome_back_idp_prompt, existingUser.getEmail(), providerName));
    mDoneButton.setOnClickListener(view -> mProvider.startSignIn(getAuth(), WelcomeBackIdpPrompt.this, providerId));
    handler.getOperation().observe(this, new ResourceObserver<IdpResponse>(this) {

        @Override
        protected void onSuccess(@NonNull IdpResponse response) {
            finish(RESULT_OK, response.toIntent());
        }

        @Override
        protected void onFailure(@NonNull Exception e) {
            if (e instanceof FirebaseAuthAnonymousUpgradeException) {
                IdpResponse response = ((FirebaseAuthAnonymousUpgradeException) e).getResponse();
                finish(ErrorCodes.ANONYMOUS_UPGRADE_MERGE_CONFLICT, response.toIntent());
            } else {
                finish(RESULT_CANCELED, IdpResponse.getErrorIntent(e));
            }
        }
    });
    TextView footerText = findViewById(R.id.email_footer_tos_and_pp_text);
    PrivacyDisclosureUtils.setupTermsOfServiceFooter(this, getFlowParams(), footerText);
}
Also used : User(com.firebase.ui.auth.data.model.User) AuthUI(com.firebase.ui.auth.AuthUI) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) TextView(android.widget.TextView) LinkingSocialProviderResponseHandler(com.firebase.ui.auth.viewmodel.idp.LinkingSocialProviderResponseHandler) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) IdpResponse(com.firebase.ui.auth.IdpResponse) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 15 with FirebaseAuthAnonymousUpgradeException

use of com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException in project FirebaseUI-Android by firebase.

the class EmailLinkCatcherActivity method initHandler.

private void initHandler() {
    mHandler = new ViewModelProvider(this).get(EmailLinkSignInHandler.class);
    mHandler.init(getFlowParams());
    mHandler.getOperation().observe(this, new ResourceObserver<IdpResponse>(this) {

        @Override
        protected void onSuccess(@NonNull IdpResponse response) {
            finish(RESULT_OK, response.toIntent());
        }

        @Override
        protected void onFailure(@NonNull final Exception e) {
            if (e instanceof UserCancellationException) {
                finish(RESULT_CANCELED, null);
            } else if (e instanceof FirebaseAuthAnonymousUpgradeException) {
                IdpResponse res = ((FirebaseAuthAnonymousUpgradeException) e).getResponse();
                finish(RESULT_CANCELED, new Intent().putExtra(ExtraConstants.IDP_RESPONSE, res));
            } else if (e instanceof FirebaseUiException) {
                int errorCode = ((FirebaseUiException) e).getErrorCode();
                if (errorCode == ErrorCodes.EMAIL_LINK_WRONG_DEVICE_ERROR || errorCode == ErrorCodes.INVALID_EMAIL_LINK_ERROR || errorCode == ErrorCodes.EMAIL_LINK_DIFFERENT_ANONYMOUS_USER_ERROR) {
                    buildAlertDialog(errorCode).show();
                } else if (errorCode == ErrorCodes.EMAIL_LINK_PROMPT_FOR_EMAIL_ERROR || errorCode == ErrorCodes.EMAIL_MISMATCH_ERROR) {
                    startErrorRecoveryFlow(RequestCodes.EMAIL_LINK_PROMPT_FOR_EMAIL_FLOW);
                } else if (errorCode == ErrorCodes.EMAIL_LINK_CROSS_DEVICE_LINKING_ERROR) {
                    startErrorRecoveryFlow(RequestCodes.EMAIL_LINK_CROSS_DEVICE_LINKING_FLOW);
                }
            } else if (e instanceof FirebaseAuthInvalidCredentialsException) {
                startErrorRecoveryFlow(RequestCodes.EMAIL_LINK_PROMPT_FOR_EMAIL_FLOW);
            } else {
                finish(RESULT_CANCELED, IdpResponse.getErrorIntent(e));
            }
        }
    });
}
Also used : UserCancellationException(com.firebase.ui.auth.data.model.UserCancellationException) FirebaseAuthInvalidCredentialsException(com.google.firebase.auth.FirebaseAuthInvalidCredentialsException) EmailLinkSignInHandler(com.firebase.ui.auth.viewmodel.email.EmailLinkSignInHandler) Intent(android.content.Intent) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) UserCancellationException(com.firebase.ui.auth.data.model.UserCancellationException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseAuthInvalidCredentialsException(com.google.firebase.auth.FirebaseAuthInvalidCredentialsException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) ViewModelProvider(androidx.lifecycle.ViewModelProvider) IdpResponse(com.firebase.ui.auth.IdpResponse)

Aggregations

FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)16 IdpResponse (com.firebase.ui.auth.IdpResponse)14 Resource (com.firebase.ui.auth.data.model.Resource)8 InOrder (org.mockito.InOrder)8 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)7 Test (org.junit.Test)7 ViewModelProvider (androidx.lifecycle.ViewModelProvider)6 AuthCredential (com.google.firebase.auth.AuthCredential)6 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)4 Intent (android.content.Intent)3 TextView (android.widget.TextView)3 AuthUI (com.firebase.ui.auth.AuthUI)3 User (com.firebase.ui.auth.data.model.User)3 UserCancellationException (com.firebase.ui.auth.data.model.UserCancellationException)3 EmailAuthCredential (com.google.firebase.auth.EmailAuthCredential)3 FakeSignInMethodQueryResult (com.firebase.ui.auth.testhelpers.FakeSignInMethodQueryResult)2 FirebaseAuthError (com.firebase.ui.auth.util.FirebaseAuthError)2 SocialProviderResponseHandler (com.firebase.ui.auth.viewmodel.idp.SocialProviderResponseHandler)2 FacebookAuthCredential (com.google.firebase.auth.FacebookAuthCredential)2 FirebaseAuthException (com.google.firebase.auth.FirebaseAuthException)2