Search in sources :

Example 6 with AuthOperationManager

use of com.firebase.ui.auth.util.data.AuthOperationManager in project FirebaseUI-Android by firebase.

the class EmailLinkSignInHandler method handleLinkingFlow.

private void handleLinkingFlow(final AuthOperationManager authOperationManager, final EmailLinkPersistenceManager persistenceManager, final IdpResponse response, final String link) {
    final AuthCredential storedCredentialForLink = ProviderUtils.getAuthCredential(response);
    final AuthCredential emailLinkCredential = EmailAuthProvider.getCredentialWithLink(response.getEmail(), link);
    if (authOperationManager.canUpgradeAnonymous(getAuth(), getArguments())) {
        authOperationManager.safeLink(emailLinkCredential, storedCredentialForLink, getArguments()).addOnCompleteListener(task -> {
            persistenceManager.clearAllData(getApplication());
            if (task.isSuccessful()) {
                handleMergeFailure(storedCredentialForLink);
            } else {
                setResult(Resource.forFailure(task.getException()));
            }
        });
    } else {
        getAuth().signInWithCredential(emailLinkCredential).continueWithTask(task -> {
            persistenceManager.clearAllData(getApplication());
            if (!task.isSuccessful()) {
                return task;
            }
            return task.getResult().getUser().linkWithCredential(storedCredentialForLink).continueWithTask(new ProfileMerger(response)).addOnFailureListener(new TaskFailureLogger(TAG, "linkWithCredential+merge failed."));
        }).addOnSuccessListener(authResult -> {
            FirebaseUser user = authResult.getUser();
            IdpResponse response1 = new IdpResponse.Builder(new User.Builder(EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD, user.getEmail()).setName(user.getDisplayName()).setPhotoUri(user.getPhotoUrl()).build()).build();
            handleSuccess(response1, authResult);
        }).addOnFailureListener(e -> setResult(Resource.forFailure(e)));
    }
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) ActionCodeResult(com.google.firebase.auth.ActionCodeResult) ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) EMAIL_LINK_PROVIDER(com.firebase.ui.auth.AuthUI.EMAIL_LINK_PROVIDER) NonNull(androidx.annotation.NonNull) User(com.firebase.ui.auth.data.model.User) EmailLinkPersistenceManager(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager) Resource(com.firebase.ui.auth.data.model.Resource) EmailLinkParser(com.firebase.ui.auth.util.data.EmailLinkParser) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) Task(com.google.android.gms.tasks.Task) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) Continuation(com.google.android.gms.tasks.Continuation) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) FirebaseUser(com.google.firebase.auth.FirebaseUser) SessionRecord(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager.SessionRecord) TextUtils(android.text.TextUtils) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) AuthCredential(com.google.firebase.auth.AuthCredential) Nullable(androidx.annotation.Nullable) EmailAuthProvider(com.google.firebase.auth.EmailAuthProvider) Application(android.app.Application) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) IdpResponse(com.firebase.ui.auth.IdpResponse) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) FirebaseUser(com.google.firebase.auth.FirebaseUser) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 7 with AuthOperationManager

use of com.firebase.ui.auth.util.data.AuthOperationManager in project FirebaseUI-Android by firebase.

the class EmailLinkSignInHandler method finishSignIn.

private void finishSignIn(@NonNull String email, @Nullable IdpResponse response) {
    if (TextUtils.isEmpty(email)) {
        setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.EMAIL_MISMATCH_ERROR)));
        return;
    }
    final AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    final EmailLinkPersistenceManager persistenceManager = EmailLinkPersistenceManager.getInstance();
    String link = getArguments().emailLink;
    if (response == null) {
        handleNormalFlow(authOperationManager, persistenceManager, email, link);
    } else {
        handleLinkingFlow(authOperationManager, persistenceManager, response, link);
    }
}
Also used : AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) EmailLinkPersistenceManager(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException)

Example 8 with AuthOperationManager

use of com.firebase.ui.auth.util.data.AuthOperationManager in project FirebaseUI-Android by firebase.

the class EmailLinkSignInHandler method handleNormalFlow.

private void handleNormalFlow(final AuthOperationManager authOperationManager, final EmailLinkPersistenceManager persistenceManager, final String email, final String link) {
    final AuthCredential emailLinkCredential = EmailAuthProvider.getCredentialWithLink(email, link);
    // Bug in core SDK - credential is mutated and won't be usable for sign in, so create
    // a new one to pass back. b/117425827
    final AuthCredential emailLinkCredentialForLinking = EmailAuthProvider.getCredentialWithLink(email, link);
    // Either regular sign in or anonymous user upgrade
    authOperationManager.signInAndLinkWithCredential(getAuth(), getArguments(), emailLinkCredential).addOnSuccessListener(authResult -> {
        persistenceManager.clearAllData(getApplication());
        FirebaseUser user = authResult.getUser();
        IdpResponse response = new IdpResponse.Builder(new User.Builder(EMAIL_LINK_PROVIDER, user.getEmail()).setName(user.getDisplayName()).setPhotoUri(user.getPhotoUrl()).build()).build();
        handleSuccess(response, authResult);
    }).addOnFailureListener(e -> {
        persistenceManager.clearAllData(getApplication());
        if (e instanceof FirebaseAuthUserCollisionException) {
            handleMergeFailure(emailLinkCredentialForLinking);
        } else {
            setResult(Resource.forFailure(e));
        }
    });
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) ActionCodeResult(com.google.firebase.auth.ActionCodeResult) ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) EMAIL_LINK_PROVIDER(com.firebase.ui.auth.AuthUI.EMAIL_LINK_PROVIDER) NonNull(androidx.annotation.NonNull) User(com.firebase.ui.auth.data.model.User) EmailLinkPersistenceManager(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager) Resource(com.firebase.ui.auth.data.model.Resource) EmailLinkParser(com.firebase.ui.auth.util.data.EmailLinkParser) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) Task(com.google.android.gms.tasks.Task) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) Continuation(com.google.android.gms.tasks.Continuation) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) FirebaseUser(com.google.firebase.auth.FirebaseUser) SessionRecord(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager.SessionRecord) TextUtils(android.text.TextUtils) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) AuthCredential(com.google.firebase.auth.AuthCredential) Nullable(androidx.annotation.Nullable) EmailAuthProvider(com.google.firebase.auth.EmailAuthProvider) Application(android.app.Application) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) IdpResponse(com.firebase.ui.auth.IdpResponse) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUser(com.google.firebase.auth.FirebaseUser) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 9 with AuthOperationManager

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

Example 10 with AuthOperationManager

use of com.firebase.ui.auth.util.data.AuthOperationManager in project FirebaseUI-Android by firebase.

the class WelcomeBackPasswordHandler method startSignIn.

/**
 * Kick off the sign-in process.
 */
public void startSignIn(@NonNull final String email, @NonNull final String password, @NonNull final IdpResponse inputResponse, @Nullable final AuthCredential credential) {
    setResult(Resource.forLoading());
    // Store the password before signing in so it can be used for later credential building
    mPendingPassword = password;
    // Build appropriate IDP response based on inputs
    final IdpResponse outputResponse;
    if (credential == null) {
        // New credential for the email provider
        outputResponse = new IdpResponse.Builder(new User.Builder(EmailAuthProvider.PROVIDER_ID, email).build()).build();
    } else {
        // New credential for an IDP (Phone or Social)
        outputResponse = new IdpResponse.Builder(inputResponse.getUser()).setPendingCredential(inputResponse.getCredentialForLinking()).setToken(inputResponse.getIdpToken()).setSecret(inputResponse.getIdpSecret()).build();
    }
    final AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    if (authOperationManager.canUpgradeAnonymous(getAuth(), getArguments())) {
        final AuthCredential credToValidate = EmailAuthProvider.getCredential(email, password);
        // Check to see if we need to link (for social providers with the same email)
        if (AuthUI.SOCIAL_PROVIDERS.contains(inputResponse.getProviderType())) {
            // Add the provider to the same account before triggering a merge failure.
            authOperationManager.safeLink(credToValidate, credential, getArguments()).addOnSuccessListener(result -> handleMergeFailure(credToValidate)).addOnFailureListener(e -> setResult(Resource.forFailure(e)));
        } else {
            // The user has not tried to log in with a federated IDP containing the same email.
            // In this case, we just need to verify that the credential they provided is valid.
            // No linking is done for non-federated IDPs.
            // A merge failure occurs because the account exists and the user is anonymous.
            authOperationManager.validateCredential(credToValidate, getArguments()).addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    handleMergeFailure(credToValidate);
                } else {
                    setResult(Resource.forFailure(task.getException()));
                }
            });
        }
    } else {
        // Kick off the flow including signing in, linking accounts, and saving with SmartLock
        getAuth().signInWithEmailAndPassword(email, password).continueWithTask(task -> {
            // Forward task failure by asking for result
            AuthResult result = task.getResult(Exception.class);
            // Task succeeded, link user if necessary
            if (credential == null) {
                return Tasks.forResult(result);
            } else {
                return result.getUser().linkWithCredential(credential).continueWithTask(new ProfileMerger(outputResponse)).addOnFailureListener(new TaskFailureLogger(TAG, "linkWithCredential+merge failed."));
            }
        }).addOnSuccessListener(result -> handleSuccess(outputResponse, result)).addOnFailureListener(e -> setResult(Resource.forFailure(e))).addOnFailureListener(new TaskFailureLogger(TAG, "signInWithEmailAndPassword failed."));
    }
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) NonNull(androidx.annotation.NonNull) User(com.firebase.ui.auth.data.model.User) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) Resource(com.firebase.ui.auth.data.model.Resource) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) 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) 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) IdpResponse(com.firebase.ui.auth.IdpResponse) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) AuthResult(com.google.firebase.auth.AuthResult) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) IdpResponse(com.firebase.ui.auth.IdpResponse)

Aggregations

AuthOperationManager (com.firebase.ui.auth.util.data.AuthOperationManager)12 IdpResponse (com.firebase.ui.auth.IdpResponse)11 AuthCredential (com.google.firebase.auth.AuthCredential)11 Resource (com.firebase.ui.auth.data.model.Resource)9 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)7 User (com.firebase.ui.auth.data.model.User)6 Test (org.junit.Test)6 Application (android.app.Application)5 NonNull (androidx.annotation.NonNull)5 Nullable (androidx.annotation.Nullable)5 SignInViewModelBase (com.firebase.ui.auth.viewmodel.SignInViewModelBase)5 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)5 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)5 AuthResult (com.google.firebase.auth.AuthResult)5 EmailAuthCredential (com.google.firebase.auth.EmailAuthCredential)5 EmailAuthProvider (com.google.firebase.auth.EmailAuthProvider)5 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)5 ErrorCodes (com.firebase.ui.auth.ErrorCodes)4 ProfileMerger (com.firebase.ui.auth.data.remote.ProfileMerger)4 ProviderUtils (com.firebase.ui.auth.util.data.ProviderUtils)4