Search in sources :

Example 11 with IdpResponse

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

the class PhoneActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fui_activity_register_phone);
    final PhoneProviderResponseHandler handler = new ViewModelProvider(this).get(PhoneProviderResponseHandler.class);
    handler.init(getFlowParams());
    handler.getOperation().observe(this, new ResourceObserver<IdpResponse>(this, R.string.fui_progress_dialog_signing_in) {

        @Override
        protected void onSuccess(@NonNull IdpResponse response) {
            startSaveCredentials(handler.getCurrentUser(), response, null);
        }

        @Override
        protected void onFailure(@NonNull Exception e) {
            handleError(e);
        }
    });
    mPhoneVerifier = new ViewModelProvider(this).get(PhoneNumberVerificationHandler.class);
    mPhoneVerifier.init(getFlowParams());
    mPhoneVerifier.onRestoreInstanceState(savedInstanceState);
    mPhoneVerifier.getOperation().observe(this, new ResourceObserver<PhoneVerification>(this, R.string.fui_verifying) {

        @Override
        protected void onSuccess(@NonNull PhoneVerification verification) {
            if (verification.isAutoVerified()) {
                Toast.makeText(PhoneActivity.this, R.string.fui_auto_verified, Toast.LENGTH_LONG).show();
                FragmentManager manager = getSupportFragmentManager();
                if (manager.findFragmentByTag(SubmitConfirmationCodeFragment.TAG) != null) {
                    // Ensure the submit code screen isn't visible if there's no code to submit.
                    // It's possible to get into this state when an SMS is sent, but then
                    // automatically retrieved.
                    manager.popBackStack();
                }
            }
            handler.startSignIn(verification.getCredential(), new IdpResponse.Builder(new User.Builder(PhoneAuthProvider.PROVIDER_ID, null).setPhoneNumber(verification.getNumber()).build()).build());
        }

        @Override
        protected void onFailure(@NonNull Exception e) {
            if (e instanceof PhoneNumberVerificationRequiredException) {
                // already be visible so we have nothing to do.
                if (getSupportFragmentManager().findFragmentByTag(SubmitConfirmationCodeFragment.TAG) == null) {
                    showSubmitCodeFragment(((PhoneNumberVerificationRequiredException) e).getPhoneNumber());
                }
                // Clear existing errors
                handleError(null);
            } else {
                handleError(e);
            }
        }
    });
    if (savedInstanceState != null) {
        return;
    }
    Bundle params = getIntent().getExtras().getBundle(ExtraConstants.PARAMS);
    CheckPhoneNumberFragment fragment = CheckPhoneNumberFragment.newInstance(params);
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_phone, fragment, CheckPhoneNumberFragment.TAG).disallowAddToBackStack().commit();
}
Also used : Bundle(android.os.Bundle) PhoneNumberVerificationRequiredException(com.firebase.ui.auth.data.model.PhoneNumberVerificationRequiredException) PhoneNumberVerificationRequiredException(com.firebase.ui.auth.data.model.PhoneNumberVerificationRequiredException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException) FragmentManager(androidx.fragment.app.FragmentManager) PhoneProviderResponseHandler(com.firebase.ui.auth.viewmodel.phone.PhoneProviderResponseHandler) ViewModelProvider(androidx.lifecycle.ViewModelProvider) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 12 with IdpResponse

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

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

the class SignInKickstarter method handleCredential.

private void handleCredential(final Credential credential) {
    String id = credential.getId();
    String password = credential.getPassword();
    if (TextUtils.isEmpty(password)) {
        String identity = credential.getAccountType();
        if (identity == null) {
            startAuthMethodChoice();
        } else {
            redirectSignIn(ProviderUtils.accountTypeToProviderId(credential.getAccountType()), id);
        }
    } else {
        final IdpResponse response = new IdpResponse.Builder(new User.Builder(EmailAuthProvider.PROVIDER_ID, id).build()).build();
        setResult(Resource.forLoading());
        getAuth().signInWithEmailAndPassword(id, password).addOnSuccessListener(result -> handleSuccess(response, result)).addOnFailureListener(e -> {
            if (e instanceof FirebaseAuthInvalidUserException || e instanceof FirebaseAuthInvalidCredentialsException) {
                // In this case the credential saved in SmartLock was not
                // a valid credential, we should delete it from SmartLock
                // before continuing.
                GoogleApiUtils.getCredentialsClient(getApplication()).delete(credential);
            }
            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) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) User(com.firebase.ui.auth.data.model.User) FirebaseAuthInvalidCredentialsException(com.google.firebase.auth.FirebaseAuthInvalidCredentialsException) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 14 with IdpResponse

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

the class AnonymousUpgradeActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBinding = ActivityAnonymousUpgradeBinding.inflate(getLayoutInflater());
    setContentView(mBinding.getRoot());
    updateUI();
    // Got here from AuthUIActivity, and we need to deal with a merge conflict
    // Occurs after catching an email link
    IdpResponse response = IdpResponse.fromResultIntent(getIntent());
    if (response != null) {
        handleSignInResult(ErrorCodes.ANONYMOUS_UPGRADE_MERGE_CONFLICT, response);
    }
    mBinding.anonSignIn.setOnClickListener(view -> signInAnonymously());
    mBinding.beginFlow.setOnClickListener(view -> startAuthUI());
    mBinding.resolveMerge.setOnClickListener(view -> resolveMerge());
    mBinding.signOut.setOnClickListener(view -> signOut());
}
Also used : IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 15 with IdpResponse

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

the class EmailLinkSignInHandlerTest method testStartSignIn_linkingFlowWithAnonymousUpgradeEnabled_failedSignInPropagated.

@Test
@SuppressWarnings("all")
public void testStartSignIn_linkingFlowWithAnonymousUpgradeEnabled_failedSignInPropagated() {
    mHandler.getOperation().observeForever(mResponseObserver);
    setupAnonymousUpgrade();
    when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
    mPersistenceManager.saveEmail(ApplicationProvider.getApplicationContext(), TestConstants.EMAIL, TestConstants.SESSION_ID, TestConstants.UID);
    mPersistenceManager.saveIdpResponseForLinking(ApplicationProvider.getApplicationContext(), buildFacebookIdpResponse());
    // Need to control FirebaseAuth's return values
    AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    authOperationManager.mScratchAuth = mScratchMockAuth;
    when(mScratchMockAuth.signInWithCredential(any(AuthCredential.class))).thenReturn(AutoContinueTask.<AuthResult>forFailure(new Exception("FAILED")));
    mHandler.startSignIn();
    // Verify sign in was called
    verify(mScratchMockAuth).signInWithCredential(any(AuthCredential.class));
    // Validate that the data was cleared
    assertThat(mPersistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext())).isNull();
    // Validate failure
    ArgumentCaptor<Resource<IdpResponse>> captor = ArgumentCaptor.forClass(Resource.class);
    InOrder inOrder = inOrder(mResponseObserver);
    inOrder.verify(mResponseObserver).onChanged(argThat(ResourceMatchers.<IdpResponse>isLoading()));
    inOrder.verify(mResponseObserver).onChanged(captor.capture());
    assertThat(captor.getValue().getException()).isNotNull();
}
Also used : FacebookAuthCredential(com.google.firebase.auth.FacebookAuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

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