Search in sources :

Example 66 with IdpResponse

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

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

the class EmailLinkFragment method onActivityCreated.

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    initHandler();
    String email = getArguments().getString(ExtraConstants.EMAIL);
    ActionCodeSettings actionCodeSettings = getArguments().getParcelable(ExtraConstants.ACTION_CODE_SETTINGS);
    IdpResponse idpResponseForLinking = getArguments().getParcelable(ExtraConstants.IDP_RESPONSE);
    boolean forceSameDevice = getArguments().getBoolean(ExtraConstants.FORCE_SAME_DEVICE);
    if (!mEmailSent) {
        mEmailLinkSendEmailHandler.sendSignInLinkToEmail(email, actionCodeSettings, idpResponseForLinking, forceSameDevice);
    }
}
Also used : ActionCodeSettings(com.google.firebase.auth.ActionCodeSettings) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 68 with IdpResponse

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

Example 69 with IdpResponse

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

the class GenericIdpSignInHandler method handleMergeFailure.

protected void handleMergeFailure(@NonNull AuthCredential credential) {
    IdpResponse failureResponse = new IdpResponse.Builder().setPendingCredential(credential).build();
    setResult(Resource.forFailure(new FirebaseAuthAnonymousUpgradeException(ErrorCodes.ANONYMOUS_UPGRADE_MERGE_CONFLICT, failureResponse)));
}
Also used : FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 70 with IdpResponse

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

the class SignedInActivity method onCreate.

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
    if (currentUser == null) {
        startActivity(AuthUiActivity.createIntent(this));
        finish();
        return;
    }
    IdpResponse response = getIntent().getParcelableExtra(ExtraConstants.IDP_RESPONSE);
    mBinding = SignedInLayoutBinding.inflate(getLayoutInflater());
    setContentView(mBinding.getRoot());
    populateProfile(response);
    populateIdpToken(response);
    mBinding.deleteAccount.setOnClickListener(view -> deleteAccountClicked());
    mBinding.signOut.setOnClickListener(view -> signOut());
}
Also used : FirebaseUser(com.google.firebase.auth.FirebaseUser) 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