Search in sources :

Example 46 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser 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 47 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project FirebaseUI-Android by firebase.

the class ChatHolder method bind.

public void bind(@NonNull AbstractChat chat) {
    setName(chat.getName());
    setMessage(chat.getMessage());
    FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
    setIsSender(currentUser != null && chat.getUid().equals(currentUser.getUid()));
}
Also used : FirebaseUser(com.google.firebase.auth.FirebaseUser)

Example 48 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project FirebaseUI-Android by firebase.

the class SignedInActivity method populateProfile.

private void populateProfile(@Nullable IdpResponse response) {
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user.getPhotoUrl() != null) {
        GlideApp.with(this).load(user.getPhotoUrl()).fitCenter().into(mBinding.userProfilePicture);
    }
    mBinding.userEmail.setText(TextUtils.isEmpty(user.getEmail()) ? "No email" : user.getEmail());
    mBinding.userPhoneNumber.setText(TextUtils.isEmpty(user.getPhoneNumber()) ? "No phone number" : user.getPhoneNumber());
    mBinding.userDisplayName.setText(TextUtils.isEmpty(user.getDisplayName()) ? "No display name" : user.getDisplayName());
    if (response == null) {
        mBinding.userIsNew.setVisibility(View.GONE);
    } else {
        mBinding.userIsNew.setVisibility(View.VISIBLE);
        mBinding.userIsNew.setText(response.isNewUser() ? "New user" : "Existing user");
    }
    List<String> providers = new ArrayList<>();
    if (user.getProviderData().isEmpty()) {
        providers.add(getString(R.string.providers_anonymous));
    } else {
        for (UserInfo info : user.getProviderData()) {
            switch(info.getProviderId()) {
                case GoogleAuthProvider.PROVIDER_ID:
                    providers.add(getString(R.string.providers_google));
                    break;
                case FacebookAuthProvider.PROVIDER_ID:
                    providers.add(getString(R.string.providers_facebook));
                    break;
                case TwitterAuthProvider.PROVIDER_ID:
                    providers.add(getString(R.string.providers_twitter));
                    break;
                case EmailAuthProvider.PROVIDER_ID:
                    providers.add(getString(R.string.providers_email));
                    break;
                case PhoneAuthProvider.PROVIDER_ID:
                    providers.add(getString(R.string.providers_phone));
                    break;
                case EMAIL_LINK_PROVIDER:
                    providers.add(getString(R.string.providers_email_link));
                    break;
                case FirebaseAuthProvider.PROVIDER_ID:
                    // Ignore this provider, it's not very meaningful
                    break;
                default:
                    providers.add(info.getProviderId());
            }
        }
    }
    mBinding.userEnabledProviders.setText(getString(R.string.used_providers, providers));
}
Also used : ArrayList(java.util.ArrayList) UserInfo(com.google.firebase.auth.UserInfo) FirebaseUser(com.google.firebase.auth.FirebaseUser)

Example 49 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project FirebaseUI-Android by firebase.

the class ProfileMerger method then.

@Override
public Task<AuthResult> then(@NonNull Task<AuthResult> task) {
    final AuthResult authResult = task.getResult();
    FirebaseUser firebaseUser = authResult.getUser();
    String name = firebaseUser.getDisplayName();
    Uri photoUri = firebaseUser.getPhotoUrl();
    if (!TextUtils.isEmpty(name) && photoUri != null) {
        return Tasks.forResult(authResult);
    }
    User user = mResponse.getUser();
    if (TextUtils.isEmpty(name)) {
        name = user.getName();
    }
    if (photoUri == null) {
        photoUri = user.getPhotoUri();
    }
    return firebaseUser.updateProfile(new UserProfileChangeRequest.Builder().setDisplayName(name).setPhotoUri(photoUri).build()).addOnFailureListener(new TaskFailureLogger(TAG, "Error updating profile")).continueWithTask(task1 -> Tasks.forResult(authResult));
}
Also used : User(com.firebase.ui.auth.data.model.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) AuthResult(com.google.firebase.auth.AuthResult) FirebaseUser(com.google.firebase.auth.FirebaseUser) Uri(android.net.Uri)

Example 50 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project FirebaseUI-Android by firebase.

the class RegisterEmailFragment method registerUser.

private void registerUser(final String email, final String name, final String password) {
    mHelper.getFirebaseAuth().createUserWithEmailAndPassword(email, password).addOnFailureListener(new TaskFailureLogger(TAG, "Error creating user")).addOnSuccessListener(new OnSuccessListener<AuthResult>() {

        @Override
        public void onSuccess(AuthResult authResult) {
            // Set display name
            UserProfileChangeRequest changeNameRequest = new UserProfileChangeRequest.Builder().setDisplayName(name).setPhotoUri(mUser.getPhotoUri()).build();
            final FirebaseUser user = authResult.getUser();
            user.updateProfile(changeNameRequest).addOnFailureListener(new TaskFailureLogger(TAG, "Error setting display name")).addOnCompleteListener(new OnCompleteListener<Void>() {

                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    // This executes even if the name change fails, since
                    // the account creation succeeded and we want to save
                    // the credential to SmartLock (if enabled).
                    mHelper.saveCredentialsOrFinish(mSaveSmartLock, getActivity(), user, password, new IdpResponse(EmailAuthProvider.PROVIDER_ID, email));
                }
            });
        }
    }).addOnFailureListener(getActivity(), new OnFailureListener() {

        @Override
        public void onFailure(@NonNull Exception e) {
            mHelper.dismissDialog();
            if (e instanceof FirebaseAuthWeakPasswordException) {
                // Password too weak
                mPasswordInput.setError(getResources().getQuantityString(R.plurals.error_weak_password, R.integer.min_password_length));
            } else if (e instanceof FirebaseAuthInvalidCredentialsException) {
                // Email address is malformed
                mEmailInput.setError(getString(R.string.invalid_email_address));
            } else if (e instanceof FirebaseAuthUserCollisionException) {
                // Collision with existing user email, it should be very hard for
                // the user to even get to this error due to CheckEmailFragment.
                mEmailInput.setError(getString(R.string.error_user_collision));
            } else {
                // General error message, this branch should not be invoked but
                // covers future API changes
                mEmailInput.setError(getString(R.string.email_account_creation_error));
            }
        }
    });
}
Also used : TaskFailureLogger(com.firebase.ui.auth.ui.TaskFailureLogger) FirebaseAuthWeakPasswordException(com.google.firebase.auth.FirebaseAuthWeakPasswordException) UserProfileChangeRequest(com.google.firebase.auth.UserProfileChangeRequest) AuthResult(com.google.firebase.auth.AuthResult) FirebaseUser(com.google.firebase.auth.FirebaseUser) FirebaseAuthWeakPasswordException(com.google.firebase.auth.FirebaseAuthWeakPasswordException) FirebaseAuthInvalidCredentialsException(com.google.firebase.auth.FirebaseAuthInvalidCredentialsException) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseAuthInvalidCredentialsException(com.google.firebase.auth.FirebaseAuthInvalidCredentialsException) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) IdpResponse(com.firebase.ui.auth.IdpResponse)

Aggregations

FirebaseUser (com.google.firebase.auth.FirebaseUser)100 AuthResult (com.google.firebase.auth.AuthResult)26 Intent (android.content.Intent)22 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)16 DataSnapshot (com.google.firebase.database.DataSnapshot)12 DatabaseError (com.google.firebase.database.DatabaseError)12 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)11 AuthCredential (com.google.firebase.auth.AuthCredential)11 DatabaseReference (com.google.firebase.database.DatabaseReference)11 ValueEventListener (com.google.firebase.database.ValueEventListener)11 View (android.view.View)10 Task (com.google.android.gms.tasks.Task)10 Button (android.widget.Button)9 NonNull (android.support.annotation.NonNull)8 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)7 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)7 TextView (android.widget.TextView)6 Test (org.junit.Test)6 Context (android.content.Context)5 SharedPreferences (android.content.SharedPreferences)5