Search in sources :

Example 61 with IdpResponse

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

the class SignInKickstarter method start.

public void start() {
    if (!TextUtils.isEmpty(getArguments().emailLink)) {
        setResult(Resource.forFailure(new IntentRequiredException(EmailLinkCatcherActivity.createIntent(getApplication(), getArguments()), RequestCodes.EMAIL_FLOW)));
        return;
    }
    // Signing in with Generic IDP puts the app in the background - it can be reclaimed by the
    // OS during the sign in flow.
    Task<AuthResult> pendingResultTask = getAuth().getPendingAuthResult();
    if (pendingResultTask != null) {
        pendingResultTask.addOnSuccessListener(authResult -> {
            final IdpResponse response = new IdpResponse.Builder(new User.Builder(authResult.getCredential().getProvider(), authResult.getUser().getEmail()).build()).build();
            handleSuccess(response, authResult);
        }).addOnFailureListener(e -> setResult(Resource.forFailure(e)));
        return;
    }
    // Only support password credentials if email auth is enabled
    boolean supportPasswords = ProviderUtils.getConfigFromIdps(getArguments().providers, EmailAuthProvider.PROVIDER_ID) != null;
    List<String> accountTypes = getCredentialAccountTypes();
    // If the request will be empty, avoid the step entirely
    boolean willRequestCredentials = supportPasswords || accountTypes.size() > 0;
    if (getArguments().enableCredentials && willRequestCredentials) {
        setResult(Resource.forLoading());
        GoogleApiUtils.getCredentialsClient(getApplication()).request(new CredentialRequest.Builder().setPasswordLoginSupported(supportPasswords).setAccountTypes(accountTypes.toArray(new String[accountTypes.size()])).build()).addOnCompleteListener(task -> {
            try {
                handleCredential(task.getResult(ApiException.class).getCredential());
            } catch (ResolvableApiException e) {
                if (e.getStatusCode() == CommonStatusCodes.RESOLUTION_REQUIRED) {
                    setResult(Resource.forFailure(new PendingIntentRequiredException(e.getResolution(), RequestCodes.CRED_HINT)));
                } else {
                    startAuthMethodChoice();
                }
            } catch (ApiException e) {
                startAuthMethodChoice();
            }
        });
    } else {
        startAuthMethodChoice();
    }
}
Also used : ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) Bundle(android.os.Bundle) EmailLinkCatcherActivity(com.firebase.ui.auth.ui.email.EmailLinkCatcherActivity) EMAIL_LINK_PROVIDER(com.firebase.ui.auth.AuthUI.EMAIL_LINK_PROVIDER) PhoneActivity(com.firebase.ui.auth.ui.phone.PhoneActivity) 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) ExtraConstants(com.firebase.ui.auth.util.ExtraConstants) GoogleApiUtils(com.firebase.ui.auth.util.GoogleApiUtils) PhoneAuthProvider(com.google.firebase.auth.PhoneAuthProvider) Task(com.google.android.gms.tasks.Task) ArrayList(java.util.ArrayList) GoogleAuthProvider(com.google.firebase.auth.GoogleAuthProvider) AuthMethodPickerActivity(com.firebase.ui.auth.ui.idp.AuthMethodPickerActivity) SingleSignInActivity(com.firebase.ui.auth.ui.idp.SingleSignInActivity) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) AuthUI(com.firebase.ui.auth.AuthUI) FirebaseAuthInvalidCredentialsException(com.google.firebase.auth.FirebaseAuthInvalidCredentialsException) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) EmailActivity(com.firebase.ui.auth.ui.email.EmailActivity) UserCancellationException(com.firebase.ui.auth.data.model.UserCancellationException) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) TextUtils(android.text.TextUtils) PendingIntentRequiredException(com.firebase.ui.auth.data.model.PendingIntentRequiredException) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) CredentialRequestResponse(com.google.android.gms.auth.api.credentials.CredentialRequestResponse) CredentialRequest(com.google.android.gms.auth.api.credentials.CredentialRequest) CommonStatusCodes(com.google.android.gms.common.api.CommonStatusCodes) List(java.util.List) Credential(com.google.android.gms.auth.api.credentials.Credential) Nullable(androidx.annotation.Nullable) EmailAuthProvider(com.google.firebase.auth.EmailAuthProvider) Application(android.app.Application) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) RequestCodes(com.firebase.ui.auth.viewmodel.RequestCodes) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) Activity(android.app.Activity) ApiException(com.google.android.gms.common.api.ApiException) IdpResponse(com.firebase.ui.auth.IdpResponse) CredentialRequest(com.google.android.gms.auth.api.credentials.CredentialRequest) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) AuthResult(com.google.firebase.auth.AuthResult) IntentRequiredException(com.firebase.ui.auth.data.model.IntentRequiredException) PendingIntentRequiredException(com.firebase.ui.auth.data.model.PendingIntentRequiredException) PendingIntentRequiredException(com.firebase.ui.auth.data.model.PendingIntentRequiredException) IdpResponse(com.firebase.ui.auth.IdpResponse) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) ApiException(com.google.android.gms.common.api.ApiException)

Example 62 with IdpResponse

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

the class EmailActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fui_activity_register_email);
    if (savedInstanceState != null) {
        return;
    }
    // Get email from intent (can be null)
    String email = getIntent().getExtras().getString(ExtraConstants.EMAIL);
    IdpResponse responseForLinking = getIntent().getExtras().getParcelable(ExtraConstants.IDP_RESPONSE);
    if (email != null && responseForLinking != null) {
        // got here from WelcomeBackEmailLinkPrompt
        AuthUI.IdpConfig emailConfig = ProviderUtils.getConfigFromIdpsOrThrow(getFlowParams().providers, EMAIL_LINK_PROVIDER);
        ActionCodeSettings actionCodeSettings = emailConfig.getParams().getParcelable(ExtraConstants.ACTION_CODE_SETTINGS);
        EmailLinkPersistenceManager.getInstance().saveIdpResponseForLinking(getApplication(), responseForLinking);
        boolean forceSameDevice = emailConfig.getParams().getBoolean(ExtraConstants.FORCE_SAME_DEVICE);
        EmailLinkFragment fragment = EmailLinkFragment.newInstance(email, actionCodeSettings, responseForLinking, forceSameDevice);
        switchFragment(fragment, R.id.fragment_register_email, EmailLinkFragment.TAG);
    } else {
        AuthUI.IdpConfig emailConfig = ProviderUtils.getConfigFromIdps(getFlowParams().providers, EmailAuthProvider.PROVIDER_ID);
        if (emailConfig != null) {
            email = emailConfig.getParams().getString(ExtraConstants.DEFAULT_EMAIL);
            ;
        }
        // Start with check email
        CheckEmailFragment fragment = CheckEmailFragment.newInstance(email);
        switchFragment(fragment, R.id.fragment_register_email, CheckEmailFragment.TAG);
    }
}
Also used : ActionCodeSettings(com.google.firebase.auth.ActionCodeSettings) AuthUI(com.firebase.ui.auth.AuthUI) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 63 with IdpResponse

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

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

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

the class AuthUiActivity method onActivityResult.

@Override
public void onActivityResult(@NonNull FirebaseAuthUIAuthenticationResult result) {
    // Successfully signed in
    IdpResponse response = result.getIdpResponse();
    handleSignInResponse(result.getResultCode(), response);
}
Also used : IdpResponse(com.firebase.ui.auth.IdpResponse)

Aggregations

IdpResponse (com.firebase.ui.auth.IdpResponse)81 Test (org.junit.Test)45 Resource (com.firebase.ui.auth.data.model.Resource)35 AuthCredential (com.google.firebase.auth.AuthCredential)31 InOrder (org.mockito.InOrder)31 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)22 FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)19 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)19 User (com.firebase.ui.auth.data.model.User)18 FirebaseUser (com.google.firebase.auth.FirebaseUser)15 AuthOperationManager (com.firebase.ui.auth.util.data.AuthOperationManager)12 EmailAuthCredential (com.google.firebase.auth.EmailAuthCredential)11 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)10 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)10 AuthResult (com.google.firebase.auth.AuthResult)10 Intent (android.content.Intent)9 Nullable (androidx.annotation.Nullable)9 IntentRequiredException (com.firebase.ui.auth.data.model.IntentRequiredException)9 Task (com.google.android.gms.tasks.Task)9 Application (android.app.Application)8