Search in sources :

Example 1 with LinkingSocialProviderResponseHandler

use of com.firebase.ui.auth.viewmodel.idp.LinkingSocialProviderResponseHandler 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 2 with LinkingSocialProviderResponseHandler

use of com.firebase.ui.auth.viewmodel.idp.LinkingSocialProviderResponseHandler in project FirebaseUI-Android by firebase.

the class LinkingSocialProviderResponseHandlerTest method setUp.

@Before
public void setUp() {
    TestHelper.initialize();
    MockitoAnnotations.initMocks(this);
    mHandler = new LinkingSocialProviderResponseHandler((Application) ApplicationProvider.getApplicationContext());
    FlowParameters testParams = TestHelper.getFlowParameters(Collections.singletonList(GoogleAuthProvider.PROVIDER_ID));
    mHandler.initializeForTesting(testParams, mMockAuth, null);
}
Also used : FlowParameters(com.firebase.ui.auth.data.model.FlowParameters) LinkingSocialProviderResponseHandler(com.firebase.ui.auth.viewmodel.idp.LinkingSocialProviderResponseHandler) Application(android.app.Application) Before(org.junit.Before)

Aggregations

LinkingSocialProviderResponseHandler (com.firebase.ui.auth.viewmodel.idp.LinkingSocialProviderResponseHandler)2 Application (android.app.Application)1 TextView (android.widget.TextView)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 AuthUI (com.firebase.ui.auth.AuthUI)1 FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)1 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)1 IdpResponse (com.firebase.ui.auth.IdpResponse)1 FlowParameters (com.firebase.ui.auth.data.model.FlowParameters)1 User (com.firebase.ui.auth.data.model.User)1 Before (org.junit.Before)1