Search in sources :

Example 16 with FirebaseUiException

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

the class SmartLockHandler method onActivityResult.

/**
 * Forward the result of a resolution from the Activity to the ViewModel.
 */
public void onActivityResult(int requestCode, int resultCode) {
    if (requestCode == RequestCodes.CRED_SAVE) {
        if (resultCode == Activity.RESULT_OK) {
            setResult(Resource.forSuccess(mResponse));
        } else {
            Log.e(TAG, "SAVE: Canceled by user.");
            FirebaseUiException exception = new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR, "Save canceled by user.");
            setResult(Resource.forFailure(exception));
        }
    }
}
Also used : FirebaseUiException(com.firebase.ui.auth.FirebaseUiException)

Example 17 with FirebaseUiException

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

the class CheckEmailFragment method onActivityCreated.

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mHandler = new ViewModelProvider(this).get(CheckEmailHandler.class);
    mHandler.init(getFlowParams());
    FragmentActivity activity = getActivity();
    if (!(activity instanceof CheckEmailListener)) {
        throw new IllegalStateException("Activity must implement CheckEmailListener");
    }
    mListener = (CheckEmailListener) activity;
    mHandler.getOperation().observe(getViewLifecycleOwner(), new ResourceObserver<User>(this, R.string.fui_progress_dialog_checking_accounts) {

        @Override
        protected void onSuccess(@NonNull User user) {
            String email = user.getEmail();
            String provider = user.getProviderId();
            mEmailEditText.setText(email);
            // noinspection ConstantConditions new user
            if (provider == null) {
                mListener.onNewUser(new User.Builder(EmailAuthProvider.PROVIDER_ID, email).setName(user.getName()).setPhotoUri(user.getPhotoUri()).build());
            } else if (provider.equals(EmailAuthProvider.PROVIDER_ID) || provider.equals(EMAIL_LINK_PROVIDER)) {
                mListener.onExistingEmailUser(user);
            } else {
                mListener.onExistingIdpUser(user);
            }
        }

        @Override
        protected void onFailure(@NonNull Exception e) {
            if (e instanceof FirebaseUiException && ((FirebaseUiException) e).getErrorCode() == ErrorCodes.DEVELOPER_ERROR) {
                mListener.onDeveloperFailure(e);
            }
            if (e instanceof FirebaseNetworkException) {
                Snackbar.make(getView(), getString(R.string.fui_no_internet), Snackbar.LENGTH_SHORT).show();
            }
        // Otherwise just let the user enter their data
        }
    });
    if (savedInstanceState != null) {
        return;
    }
    // Check for email
    String email = getArguments().getString(ExtraConstants.EMAIL);
    if (!TextUtils.isEmpty(email)) {
        mEmailEditText.setText(email);
        validateAndProceed();
    } else if (getFlowParams().enableHints) {
        mHandler.fetchCredential();
    }
}
Also used : User(com.firebase.ui.auth.data.model.User) FirebaseNetworkException(com.google.firebase.FirebaseNetworkException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FragmentActivity(androidx.fragment.app.FragmentActivity) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) ViewModelProvider(androidx.lifecycle.ViewModelProvider) FirebaseNetworkException(com.google.firebase.FirebaseNetworkException)

Example 18 with FirebaseUiException

use of com.firebase.ui.auth.FirebaseUiException 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 19 with FirebaseUiException

use of com.firebase.ui.auth.FirebaseUiException 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 20 with FirebaseUiException

use of com.firebase.ui.auth.FirebaseUiException 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)

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