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)));
}
}
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()));
}
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));
}
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));
}
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));
}
}
});
}
Aggregations