Search in sources :

Example 36 with Resource

use of com.firebase.ui.auth.data.model.Resource in project FirebaseUI-Android by firebase.

the class LinkingSocialProviderResponseHandler method startSignIn.

public void startSignIn(@NonNull final IdpResponse response) {
    if (!response.isSuccessful()) {
        setResult(Resource.forFailure(response.getError()));
        return;
    }
    if (isInvalidProvider(response.getProviderType())) {
        throw new IllegalStateException("This handler cannot be used to link email or phone providers.");
    }
    if (mEmail != null && !mEmail.equals(response.getEmail())) {
        setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.EMAIL_MISMATCH_ERROR)));
        return;
    }
    setResult(Resource.forLoading());
    // return a credential.
    if (isGenericIdpLinkingFlow(response.getProviderType())) {
        getAuth().getCurrentUser().linkWithCredential(mRequestedSignInCredential).addOnSuccessListener(authResult -> handleSuccess(response, authResult)).addOnFailureListener(e -> Resource.<IdpResponse>forFailure(e));
        return;
    }
    final AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    final AuthCredential credential = ProviderUtils.getAuthCredential(response);
    if (authOperationManager.canUpgradeAnonymous(getAuth(), getArguments())) {
        if (mRequestedSignInCredential == null) {
            // The user has provided a valid credential by signing in with a federated
            // idp. linkWithCredential will fail because the user is anonymous and the account
            // exists (we're in the welcome back flow).
            // We know that they are signing in with the same IDP because requestSignInCredential
            // is null.
            // We just need to have the developer handle the merge failure.
            handleMergeFailure(credential);
        } else {
            // The user has logged in with an IDP that has the same email with another IDP
            // present on the account.
            // These IDPs belong to the same account - they must be linked, but we can't lose
            // our anonymous user session
            authOperationManager.safeLink(credential, mRequestedSignInCredential, getArguments()).addOnSuccessListener(result -> handleMergeFailure(credential)).addOnFailureListener(e -> setResult(Resource.forFailure(e)));
        }
    } else {
        getAuth().signInWithCredential(credential).continueWithTask(task -> {
            final AuthResult result = task.getResult();
            if (mRequestedSignInCredential == null) {
                return Tasks.forResult(result);
            } else {
                return result.getUser().linkWithCredential(mRequestedSignInCredential).continueWith(task1 -> {
                    if (task1.isSuccessful()) {
                        return task1.getResult();
                    } else {
                        // to backtrack so we just ignore any errors.
                        return result;
                    }
                });
            }
        }).addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                handleSuccess(response, task.getResult());
            } else {
                setResult(Resource.forFailure(task.getException()));
            }
        });
    }
}
Also used : ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) NonNull(androidx.annotation.NonNull) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) TextUtils(android.text.TextUtils) Resource(com.firebase.ui.auth.data.model.Resource) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) PhoneAuthProvider(com.google.firebase.auth.PhoneAuthProvider) Task(com.google.android.gms.tasks.Task) AuthCredential(com.google.firebase.auth.AuthCredential) Tasks(com.google.android.gms.tasks.Tasks) Nullable(androidx.annotation.Nullable) RestrictTo(androidx.annotation.RestrictTo) EmailAuthProvider(com.google.firebase.auth.EmailAuthProvider) Application(android.app.Application) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) AuthUI(com.firebase.ui.auth.AuthUI) Continuation(com.google.android.gms.tasks.Continuation) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) IdpResponse(com.firebase.ui.auth.IdpResponse) AuthCredential(com.google.firebase.auth.AuthCredential) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) AuthResult(com.google.firebase.auth.AuthResult) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException)

Aggregations

Resource (com.firebase.ui.auth.data.model.Resource)36 InOrder (org.mockito.InOrder)35 Test (org.junit.Test)34 IdpResponse (com.firebase.ui.auth.IdpResponse)28 AuthCredential (com.google.firebase.auth.AuthCredential)20 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)13 FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)11 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)9 EmailAuthCredential (com.google.firebase.auth.EmailAuthCredential)9 FacebookAuthCredential (com.google.firebase.auth.FacebookAuthCredential)8 OAuthProvider (com.google.firebase.auth.OAuthProvider)7 Activity (android.app.Activity)6 FakeSignInMethodQueryResult (com.firebase.ui.auth.testhelpers.FakeSignInMethodQueryResult)6 AuthOperationManager (com.firebase.ui.auth.util.data.AuthOperationManager)5 IntentRequiredException (com.firebase.ui.auth.data.model.IntentRequiredException)3 ActionCodeSettings (com.google.firebase.auth.ActionCodeSettings)3 GoogleAuthCredential (com.google.firebase.auth.GoogleAuthCredential)3 FirebaseUiUserCollisionException (com.firebase.ui.auth.FirebaseUiUserCollisionException)2 User (com.firebase.ui.auth.data.model.User)2 FirebaseUser (com.google.firebase.auth.FirebaseUser)2