Not Showing DisplayName firebase Android? - android

Everything worked but not showing name only.
My code is:
user = profAuth.getCurrentUser();
if (user != null) {
// Name, email address
String uid = user.getUid();
String name = user.getDisplayName();
String email = user.getEmail();
txtName.setText(name);
txtEmail.setText(email);
txtUserid.setText(uid);
}

That's because Firebase Auth doesn't prompt the user to provide a Display name when signing up with Email/Password. But you can do that manually. Prompt the user to type the display name he desires, and pass it to the setDisplayName() method bellow:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(desiredName)
.build();
user.updateProfile(profileUpdates)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User display name updated.");
}
}
});

Related

How to assign displayName?

I've registered an account in my Android application using Firebase and there are multiple textfields.
How can I assign particular textfield as displayName of user so that I can later access it using getDisplayName()?
To change a user's display name, you need to update the user profile:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName("Jane Q. User")
.setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg"))
.build();
user.updateProfile(profileUpdates)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User profile updated.");
}
}
});

How to get UserName from FirebaseUser using authentication Uid in android?

I registered a user by using createUserWithEmailAndPassword() and login using signInWithEmailAndPassword() methods. Now when I login a user I need to get the username, mobile, that are stored in the user node. I could get the UId for each user, by using this how it possible to get the mentioned information in android?
all you have to do is use UserProfileChangeRequest
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
// Sign in success
FirebaseUser user = mAuth.getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(mName).build();
user.updateProfile(profileUpdates).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User profile updated.");
}
}
});
}
});
Then, to retrieve it, use this wherever required,
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// Name, email address etc
String name = user.getDisplayName();
String email = user.getEmail();
}
or you can create your own custom node.!
String currentuser = FirebaseAuth.getInstance().getUid();
//save user node after successful signup.
mFirebaseInstance.getReference("user").child(scurrentuser ).setValue(parameters);
databaseReference = FirebaseDatabase.getInstance().getReference().child("user").child(currentuser );
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot userSnapShot: dataSnapshot.getChildren()){
User user = userSnapShot.getValue(User.class);
}
}

Firebase : Get Facebook Photo Url [ Android ]

Im using Facebook Authentication with Firebase and I'm trying to get the Photo Url from the authenticated user. I've tried using the following method:
String photoUrl = firebaseUser.getPhotoUrl().toString();
And this method :
String photoUrl = firebaseUser.getProviderData().get(0).getPhotoUrl().toString();
They both return a url in the format : https://scontent.xx.fbcdn.net/v/t1.0-1/p100x100/1230538156_10205381561684678_351630538156623_n.jpg
When I try to access this url, I get the following message:
"Access to scontent.xx.fbcdn.net was denied"
As an alternative, I have tried getting the user's facebook id so I can request the photo using the Graph API but seems to be no way to get the facebook id. I thought the following would work:
firebaseUser.getProviderData().get(0).getProviderId();
But this returns the string "firebase" ??? ...umm what?
My full method:
private void firebaseAuthWithFacebook(AccessToken token) {
showProgressDialog("Signing in with Facebook","Signing in with Facebook");
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
if(task.isSuccessful()){
FirebaseUser user = mAuth.getInstance().getCurrentUser();
if (user != null) {
addNewUserFacebook(user);
}
}
else{
Toast.makeText(SignInActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
}
hideProgressDialog();
}
});
}
private void addNewUserFacebook(FirebaseUser firebaseUser){
String name = firebaseUser.getDisplayName();
String email = firebaseUser.getEmail();
/*My THREE failed methods - cue sad trombone */
//String photoUrl = firebaseUser.getPhotoUrl().toString();
//String photoUrl = firebaseUser.getProviderData().get(0).getPhotoUrl().toString();
String photoUrl = "https://graph.facebook.com/" + firebaseUser.getProviderData().get(0).getProviderId() + "/picture?type=large";
String providerId = "Facebook";
final String uid = firebaseUser.getUid();
User user = new User(name, email, photoUrl, providerId);
rootRef.child("users").child(uid).setValue(user, new DatabaseReference.CompletionListener() {
#Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError == null){
//add local user information
updateUser(uid);
}
}
});
}
Well, I had the same problem, I deleted all the data from the Facebook application in the configuration on the phone, no data, I tried and the problem solved, Facebook gets dizzy I think.
this method works for me
String photoUrl = firebaseUser.getProviderData().get(0).getPhotoUrl().toString();

Can't update user display name and photo url in Firebase

I used this code sinppet to update the current Firebase user which have signed up using createUserWithEmailAndPassword() method:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName("Jane Q. User")
.setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg"))
.build();
user.updateProfile(profileUpdates)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User profile updated.");
}
}
});
But when I try retrieve the diplayName or photoUri, they return null as if they've never been set in the first place.

Create new user with Names , Username etc in firebase

I'm new android learner so its is difficult for me to do stuffs which I cannot find in the documentation. Here is my code for creating users
mAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
//Successfully Registered
Toast.makeText(RegisterActivity.this, "Registration Successful", Toast.LENGTH_SHORT).show();
}else {
//Error occurred during registration
Toast.makeText(RegisterActivity.this, "Registration Unsuccessful", Toast.LENGTH_SHORT).show();
try {
throw task.getException();
} catch(FirebaseAuthWeakPasswordException e) {
editTextPassword.setError(e.getMessage());
editTextPassword.requestFocus();
}catch(FirebaseAuthUserCollisionException | FirebaseAuthInvalidCredentialsException e) {
editTextEmail.setError(e.getMessage());
editTextEmail.requestFocus();
} catch(Exception e) {
Log.e(RegisterActivity.class.getName(), e.getMessage());
}
}
progressDialog.dismiss();
}
});
This only takes two parameters(email and password) to create an user. To create user with more fields what approach should I take.
I have also added a FirebaseAuth.AuthStateListener() to check user login status. But when I'm calling firebaseAuth.getCurrentUser().getDisplayName() after successfully user login it returns null as usual.So how can I create user with Names so I can retrieve it with firebaseAuth.getCurrentUser().getDisplayName().
After the registration is successful,
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName("Jane Q. User")
.build();
EDIT: This code is a bit incomplete as profileUpdates is never accessed.
user.updateProfile(profileUpdates)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User profile updated.");
}
}
});
Then, to retrieve it, use this wherever required,
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// Name, email address etc
String name = user.getDisplayName();
String email = user.getEmail();
}
To add a user with extra information such as User's name or other required information you should store these data using the Firebase real-time database under the unique user_id generated upon successful completion of email and password registration.
Get user input for name in registration form,
String name = mNameField.getText().toString().trim();
Add user's name in onComplete method :
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
String user_id = mAuth.getCurrentUser().getUid;
DatabaseReference current_user = mDatabase.child(user_id);
current_user.child("name").setValue(name);
progressDialog.dismiss();
}

Categories

Resources