Search in sources :

Example 1 with FirebaseAuthError

use of com.firebase.ui.auth.util.FirebaseAuthError 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 2 with FirebaseAuthError

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

the class SocialProviderResponseHandler method startSignIn.

public void startSignIn(@NonNull final IdpResponse response) {
    if (!response.isSuccessful() && !response.isRecoverableErrorResponse()) {
        setResult(Resource.forFailure(response.getError()));
        return;
    }
    if (isEmailOrPhoneProvider(response.getProviderType())) {
        throw new IllegalStateException("This handler cannot be used with email or phone providers");
    }
    setResult(Resource.forLoading());
    // a credential made from the id token/access token cannot be used to sign-in.
    if (response.hasCredentialForLinking()) {
        handleGenericIdpLinkingFlow(response);
        return;
    }
    final AuthCredential credential = ProviderUtils.getAuthCredential(response);
    AuthOperationManager.getInstance().signInAndLinkWithCredential(getAuth(), getArguments(), credential).continueWithTask(new ProfileMerger(response)).addOnSuccessListener(result -> handleSuccess(response, result)).addOnFailureListener(e -> {
        // For some reason disabled users can hit FirebaseAuthUserCollisionException
        // so we have to handle this special case.
        boolean isDisabledUser = (e instanceof FirebaseAuthInvalidUserException);
        if (e instanceof FirebaseAuthException) {
            FirebaseAuthException authEx = (FirebaseAuthException) e;
            FirebaseAuthError fae = FirebaseAuthError.fromException(authEx);
            if (fae == FirebaseAuthError.ERROR_USER_DISABLED) {
                isDisabledUser = true;
            }
        }
        if (isDisabledUser) {
            setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.ERROR_USER_DISABLED)));
        } else if (e instanceof FirebaseAuthUserCollisionException) {
            final String email = response.getEmail();
            if (email == null) {
                setResult(Resource.forFailure(e));
                return;
            }
            // There can be a collision due to:
            // CASE 1: Anon user signing in with a credential that belongs to an
            // existing user.
            // CASE 2: non - anon user signing in with a credential that does not
            // belong to an existing user, but the email matches an existing user
            // that has another social IDP. We need to link this new IDP to this
            // existing user.
            // CASE 3: CASE 2 with an anonymous user. We link the new IDP to the
            // same account before handling invoking a merge failure.
            ProviderUtils.fetchSortedProviders(getAuth(), getArguments(), email).addOnSuccessListener(providers -> {
                if (providers.contains(response.getProviderType())) {
                    // Case 1
                    handleMergeFailure(credential);
                } else if (providers.isEmpty()) {
                    setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.DEVELOPER_ERROR, "No supported providers.")));
                } else {
                    // Case 2 & 3 - we need to link
                    startWelcomeBackFlowForLinking(providers.get(0), response);
                }
            }).addOnFailureListener(e1 -> setResult(Resource.forFailure(e1)));
        }
    });
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) WelcomeBackIdpPrompt(com.firebase.ui.auth.ui.idp.WelcomeBackIdpPrompt) ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) EMAIL_LINK_PROVIDER(com.firebase.ui.auth.AuthUI.EMAIL_LINK_PROVIDER) NonNull(androidx.annotation.NonNull) User(com.firebase.ui.auth.data.model.User) IntentRequiredException(com.firebase.ui.auth.data.model.IntentRequiredException) Intent(android.content.Intent) Resource(com.firebase.ui.auth.data.model.Resource) PhoneAuthProvider(com.google.firebase.auth.PhoneAuthProvider) RestrictTo(androidx.annotation.RestrictTo) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) TextUtils(android.text.TextUtils) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) FirebaseAuthError(com.firebase.ui.auth.util.FirebaseAuthError) AuthCredential(com.google.firebase.auth.AuthCredential) List(java.util.List) Nullable(androidx.annotation.Nullable) EmailAuthProvider(com.google.firebase.auth.EmailAuthProvider) Application(android.app.Application) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) WelcomeBackEmailLinkPrompt(com.firebase.ui.auth.ui.email.WelcomeBackEmailLinkPrompt) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException) RequestCodes(com.firebase.ui.auth.viewmodel.RequestCodes) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) Activity(android.app.Activity) IdpResponse(com.firebase.ui.auth.IdpResponse) WelcomeBackPasswordPrompt(com.firebase.ui.auth.ui.email.WelcomeBackPasswordPrompt) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) FirebaseAuthError(com.firebase.ui.auth.util.FirebaseAuthError) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException)

Example 3 with FirebaseAuthError

use of com.firebase.ui.auth.util.FirebaseAuthError 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 4 with FirebaseAuthError

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

the class GenericIdpSignInHandler method handleNormalSignInFlow.

protected void handleNormalSignInFlow(final FirebaseAuth auth, final HelperActivityBase activity, final OAuthProvider provider) {
    final boolean useEmulator = activity.getAuthUI().isUseEmulator();
    auth.startActivityForSignInWithProvider(activity, provider).addOnSuccessListener(authResult -> handleSuccess(useEmulator, provider.getProviderId(), authResult.getUser(), (OAuthCredential) authResult.getCredential(), authResult.getAdditionalUserInfo().isNewUser())).addOnFailureListener(e -> {
        if (e instanceof FirebaseAuthException) {
            FirebaseAuthError error = FirebaseAuthError.fromException((FirebaseAuthException) e);
            if (e instanceof FirebaseAuthUserCollisionException) {
                FirebaseAuthUserCollisionException collisionException = (FirebaseAuthUserCollisionException) e;
                setResult(Resource.forFailure(new FirebaseUiUserCollisionException(ErrorCodes.ERROR_GENERIC_IDP_RECOVERABLE_ERROR, "Recoverable error.", provider.getProviderId(), collisionException.getEmail(), collisionException.getUpdatedCredential())));
            } else if (error == FirebaseAuthError.ERROR_WEB_CONTEXT_CANCELED) {
                setResult(Resource.forFailure(new UserCancellationException()));
            } else {
                setResult(Resource.forFailure(e));
            }
        } else {
            setResult(Resource.forFailure(e));
        }
    });
}
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) UserCancellationException(com.firebase.ui.auth.data.model.UserCancellationException) FirebaseAuthError(com.firebase.ui.auth.util.FirebaseAuthError) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException) FirebaseUiUserCollisionException(com.firebase.ui.auth.FirebaseUiUserCollisionException)

Aggregations

FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)4 IdpResponse (com.firebase.ui.auth.IdpResponse)4 FirebaseAuthError (com.firebase.ui.auth.util.FirebaseAuthError)4 FirebaseAuthException (com.google.firebase.auth.FirebaseAuthException)4 FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)3 Application (android.app.Application)2 Intent (android.content.Intent)2 NonNull (androidx.annotation.NonNull)2 Nullable (androidx.annotation.Nullable)2 RestrictTo (androidx.annotation.RestrictTo)2 ErrorCodes (com.firebase.ui.auth.ErrorCodes)2 Resource (com.firebase.ui.auth.data.model.Resource)2 User (com.firebase.ui.auth.data.model.User)2 AuthOperationManager (com.firebase.ui.auth.util.data.AuthOperationManager)2 ProviderUtils (com.firebase.ui.auth.util.data.ProviderUtils)2 RequestCodes (com.firebase.ui.auth.viewmodel.RequestCodes)2 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)2 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)2 AuthCredential (com.google.firebase.auth.AuthCredential)2 AuthResult (com.google.firebase.auth.AuthResult)2