Search in sources :

Example 1 with ProfileMerger

use of com.firebase.ui.auth.data.remote.ProfileMerger in project FirebaseUI-Android by firebase.

the class EmailProviderResponseHandler method startSignIn.

public void startSignIn(@NonNull final IdpResponse response, @NonNull final String password) {
    if (!response.isSuccessful()) {
        setResult(Resource.forFailure(response.getError()));
        return;
    }
    if (!response.getProviderType().equals(EmailAuthProvider.PROVIDER_ID)) {
        throw new IllegalStateException("This handler can only be used with the email provider");
    }
    setResult(Resource.forLoading());
    final AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    final String email = response.getEmail();
    authOperationManager.createOrLinkUserWithEmailAndPassword(getAuth(), getArguments(), email, password).continueWithTask(new ProfileMerger(response)).addOnFailureListener(new TaskFailureLogger(TAG, "Error creating user")).addOnSuccessListener(result -> handleSuccess(response, result)).addOnFailureListener(e -> {
        if (e instanceof FirebaseAuthUserCollisionException) {
            if (authOperationManager.canUpgradeAnonymous(getAuth(), getArguments())) {
                AuthCredential credential = EmailAuthProvider.getCredential(email, password);
                handleMergeFailure(credential);
            } else {
                Log.w(TAG, "Got a collision error during a non-upgrade flow", e);
                // Collision with existing user email without anonymous upgrade
                // it should be very hard for the user to even get to this error
                // due to CheckEmailFragment.
                ProviderUtils.fetchTopProvider(getAuth(), getArguments(), email).addOnSuccessListener(new StartWelcomeBackFlow(email)).addOnFailureListener(e1 -> setResult(Resource.forFailure(e1)));
            }
        } else {
            setResult(Resource.forFailure(e));
        }
    });
}
Also used : WelcomeBackIdpPrompt(com.firebase.ui.auth.ui.idp.WelcomeBackIdpPrompt) 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) IntentRequiredException(com.firebase.ui.auth.data.model.IntentRequiredException) Resource(com.firebase.ui.auth.data.model.Resource) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) RestrictTo(androidx.annotation.RestrictTo) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) Log(android.util.Log) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) 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) WelcomeBackEmailLinkPrompt(com.firebase.ui.auth.ui.email.WelcomeBackEmailLinkPrompt) RequestCodes(com.firebase.ui.auth.viewmodel.RequestCodes) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) IdpResponse(com.firebase.ui.auth.IdpResponse) WelcomeBackPasswordPrompt(com.firebase.ui.auth.ui.email.WelcomeBackPasswordPrompt) AuthCredential(com.google.firebase.auth.AuthCredential) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException)

Example 2 with ProfileMerger

use of com.firebase.ui.auth.data.remote.ProfileMerger in project FirebaseUI-Android by firebase.

the class SocialProviderResponseHandler method startSignIn.

public void startSignIn(@NonNull final IdpResponse response) {
    if (!response.isSuccessful() && !response.isRecoverableErrorResponse()) {
        setResult(Resource.forFailure(response.getError()));
        return;
    }
    if (isEmailOrPhoneProvider(response.getProviderType())) {
        throw new IllegalStateException("This handler cannot be used with email or phone providers");
    }
    setResult(Resource.forLoading());
    // a credential made from the id token/access token cannot be used to sign-in.
    if (response.hasCredentialForLinking()) {
        handleGenericIdpLinkingFlow(response);
        return;
    }
    final AuthCredential credential = ProviderUtils.getAuthCredential(response);
    AuthOperationManager.getInstance().signInAndLinkWithCredential(getAuth(), getArguments(), credential).continueWithTask(new ProfileMerger(response)).addOnSuccessListener(result -> handleSuccess(response, result)).addOnFailureListener(e -> {
        // For some reason disabled users can hit FirebaseAuthUserCollisionException
        // so we have to handle this special case.
        boolean isDisabledUser = (e instanceof FirebaseAuthInvalidUserException);
        if (e instanceof FirebaseAuthException) {
            FirebaseAuthException authEx = (FirebaseAuthException) e;
            FirebaseAuthError fae = FirebaseAuthError.fromException(authEx);
            if (fae == FirebaseAuthError.ERROR_USER_DISABLED) {
                isDisabledUser = true;
            }
        }
        if (isDisabledUser) {
            setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.ERROR_USER_DISABLED)));
        } else if (e instanceof FirebaseAuthUserCollisionException) {
            final String email = response.getEmail();
            if (email == null) {
                setResult(Resource.forFailure(e));
                return;
            }
            // There can be a collision due to:
            // CASE 1: Anon user signing in with a credential that belongs to an
            // existing user.
            // CASE 2: non - anon user signing in with a credential that does not
            // belong to an existing user, but the email matches an existing user
            // that has another social IDP. We need to link this new IDP to this
            // existing user.
            // CASE 3: CASE 2 with an anonymous user. We link the new IDP to the
            // same account before handling invoking a merge failure.
            ProviderUtils.fetchSortedProviders(getAuth(), getArguments(), email).addOnSuccessListener(providers -> {
                if (providers.contains(response.getProviderType())) {
                    // Case 1
                    handleMergeFailure(credential);
                } else if (providers.isEmpty()) {
                    setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.DEVELOPER_ERROR, "No supported providers.")));
                } else {
                    // Case 2 & 3 - we need to link
                    startWelcomeBackFlowForLinking(providers.get(0), response);
                }
            }).addOnFailureListener(e1 -> setResult(Resource.forFailure(e1)));
        }
    });
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) WelcomeBackIdpPrompt(com.firebase.ui.auth.ui.idp.WelcomeBackIdpPrompt) 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) IntentRequiredException(com.firebase.ui.auth.data.model.IntentRequiredException) Intent(android.content.Intent) Resource(com.firebase.ui.auth.data.model.Resource) PhoneAuthProvider(com.google.firebase.auth.PhoneAuthProvider) RestrictTo(androidx.annotation.RestrictTo) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) TextUtils(android.text.TextUtils) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) FirebaseAuthError(com.firebase.ui.auth.util.FirebaseAuthError) AuthCredential(com.google.firebase.auth.AuthCredential) List(java.util.List) 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) WelcomeBackEmailLinkPrompt(com.firebase.ui.auth.ui.email.WelcomeBackEmailLinkPrompt) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException) RequestCodes(com.firebase.ui.auth.viewmodel.RequestCodes) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) Activity(android.app.Activity) IdpResponse(com.firebase.ui.auth.IdpResponse) WelcomeBackPasswordPrompt(com.firebase.ui.auth.ui.email.WelcomeBackPasswordPrompt) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) FirebaseAuthError(com.firebase.ui.auth.util.FirebaseAuthError) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException)

Example 3 with ProfileMerger

use of com.firebase.ui.auth.data.remote.ProfileMerger 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 4 with ProfileMerger

use of com.firebase.ui.auth.data.remote.ProfileMerger 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

Application (android.app.Application)4 NonNull (androidx.annotation.NonNull)4 Nullable (androidx.annotation.Nullable)4 IdpResponse (com.firebase.ui.auth.IdpResponse)4 Resource (com.firebase.ui.auth.data.model.Resource)4 User (com.firebase.ui.auth.data.model.User)4 ProfileMerger (com.firebase.ui.auth.data.remote.ProfileMerger)4 AuthOperationManager (com.firebase.ui.auth.util.data.AuthOperationManager)4 SignInViewModelBase (com.firebase.ui.auth.viewmodel.SignInViewModelBase)4 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)4 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)4 AuthCredential (com.google.firebase.auth.AuthCredential)4 AuthResult (com.google.firebase.auth.AuthResult)4 EmailAuthProvider (com.google.firebase.auth.EmailAuthProvider)4 RestrictTo (androidx.annotation.RestrictTo)3 EMAIL_LINK_PROVIDER (com.firebase.ui.auth.AuthUI.EMAIL_LINK_PROVIDER)3 ErrorCodes (com.firebase.ui.auth.ErrorCodes)3 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)3 ProviderUtils (com.firebase.ui.auth.util.data.ProviderUtils)3 TaskFailureLogger (com.firebase.ui.auth.util.data.TaskFailureLogger)3