I am trying to update my email from FireBase, how can I achive this? updateEmail looks like has been depreacated?
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
AuthCredential credential= EmailAuthProvider.getCredential(user.getEmail(),edtPassword.getText().toString());
user.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(#NonNull Task task) {
if(task.isSuccessful()){
FirebaseAuth.getInstance().fetchSignInMethodsForEmail(edtEmail.getText().toString())
.addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
#Override
public void onComplete(#NonNull Task<SignInMethodQueryResult> task) {
if(task.isSuccessful()){
if(task.getResult().getSignInMethods().size()==0){
Here--------------------------------->
}else {
Toast.makeText(AccountSettingsActivity.this,"The Email is alread in use",Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(AccountSettingsActivity.this,"Task is not successfull in fetch",Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
progressBar.setVisibility(View.GONE);
Toast.makeText(AccountSettingsActivity.this,"Unable to edt email",Toast.LENGTH_SHORT).show();
}
});
}else {
Toast.makeText(AccountSettingsActivity.this,"Task is not successfull", Toast.LENGTH_LONG).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
progressBar.setVisibility(View.GONE);
Toast.makeText(AccountSettingsActivity.this,"Unable to update email failure",Toast.LENGTH_LONG).show();
}
});
}
I had the same issu and I found the solution here : How to update email from Firebase in Android?
To retrieve password for credential you can use SharedPreferences.
It worked for me ;)
Related
i want to accsess my firebase collection but somehow it didnt work on this java file, in otherwise i had another java file on my android project thats containts the same syntax and its actually work.. i dont have any idea to solve this line
on Signupn.java (worked)
fAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(SignUp.this, "User Created", Toast.LENGTH_SHORT).show();
userId = fAuth.getCurrentUser().getUid();
DocumentReference documentReference =fStore.collection("users").document(userId);
Map<String,Object> user =new HashMap<>();
user.put("fName",fullName);
user.put("email",email);
user.put("phone",phone);
documentReference.set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void unused) {
Log.d(TAG,"onSuccess: user Profile is created for "+ userId);
}
});
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}else{
Toast.makeText(SignUp.this, "Error!"+task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
on EditProfile.java (error)
saveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editFullName.getText().toString().isEmpty() || editEmail.getText().toString().isEmpty()
|| editPhone.getText().toString().isEmpty()) {
Toast.makeText(EditProfile.this, "All Fields Are Required.", Toast.LENGTH_SHORT).show();
return;
}
String email = editEmail.getText().toString();
user.updateEmail(email).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void unused) {
DocumentReference docRef = fStore.collection("users").document(user.getUid());
Map<String, Object> edited = new HashMap<>();
edited.put("email", email);
edited.put("fName", editFullName.getText().toString());
edited.put("phone", editPhone.getText().toString());
docRef.update(edited);
Toast.makeText(EditProfile.this, "Email Changed Successfully.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(EditProfile.this, "Error!", Toast.LENGTH_SHORT).show();
}
});
}
});
here is where the error line
DocumentReference docRef = fStore.collection("users").document(user.getUid());
in collection sentence.
this picture from editProfile.java
this picture from signup.java
thank you for your value answers, any answer i really apriciate it.
Please verify your dataStype for fstore object in both classes or post full code wherever you are declaring the fstore and intializing it
This is all the code with register account with email password, save verify email, save user data into Firestore database. Only the Firestore database can't run.
fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
// send verification link
FirebaseUser fuser = fAuth.getCurrentUser();
fuser.sendEmailVerification().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(register.this, "Verification Email Has been Sent.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.d(TAG, "onFailure: Email not sent " + e.getMessage());
}
});
Toast.makeText(register.this, "User Created.", Toast.LENGTH_SHORT).show();
userID = fAuth.getCurrentUser().getUid();
DocumentReference documentReference = fStore.collection("users").document(userID);
Map<String,Object> user = new HashMap<>();
user.put("fName",fullName);
user.put("email",email);
user.put("phone",phone);
documentReference.set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: user Profile is created for "+ userID);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w(TAG, "onFailure: " + e.toString());
}
});
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}
else {
Toast.makeText(register.this, "Error ! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
});
The Firebase authentication operation is asynchronous. This means that you can be sure that authentication succeeded only when the onSuccess() method fires. Since you say that the authentication process successfully completes, your first onSuccess() is triggered. Your second onSuccess() is not triggered because the code that corresponds to the addition of data to Firestore is outside the callback, hence that behavior. To solve this, all the code that exists outside the callback should be moved inside the first onSuccess() as explained in the following line of code:
fAuth
.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
// send verification link
FirebaseUser fuser = fAuth.getCurrentUser();
fuser.sendEmailVerification().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(register.this, "Verification Email Has been Sent.", Toast.LENGTH_SHORT).show();
Toast.makeText(register.this, "User Created.", Toast.LENGTH_SHORT).show();
//Moved code 👈👈👈
userID = fAuth.getCurrentUser().getUid();
DocumentReference documentReference = fStore.collection("users").document(userID);
Map<String,Object> user = new HashMap<>();
user.put("fName",fullName);
user.put("email",email);
user.put("phone",phone);
documentReference.set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: user Profile is created for "+ userID);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w(TAG, "onFailure: " + e.toString());
}
});
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.d(TAG, "onFailure: Email not sent " + e.getMessage());
}
});
} else {
Toast.makeText(register.this, "Error ! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
In this way, you be able to write the data to Firestore when the authentication process is completed.
Below are the code. I don't have much experience in coding. Hope you could help me out! Thank you so much=)
My Error are :
error: incompatible types: > cannot be converted to OnCompleteListener
//Register new User
auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
.addOnCompleteListener(new OnSuccessListener<AuthResult>() {
#Override
public void onSuccess(AuthResult authResult) {
//Save user to db
User user = new User();
user.setEmail(edtEmail.getText().toString());
user.setName(edtName.getText().toString());
user.setPhone(edtPhone.getText().toString());
user.setPassword(edtPassword.getText().toString());
//User email to key
users.child(user.getEmail())
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>(){
#Override
public void onSuccess(Void aVoid) {
Snackbar.make(rootLayout, "Register successful !!! ", Snackbar.LENGTH_SHORT)
.show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Snackbar.make(rootLayout,"Failed "+e.getMessage(),Snackbar.LENGTH_SHORT)
.show();
}
})
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Snackbar.make(rootLayout,"Failed "+e.getMessage(),Snackbar.LENGTH_SHORT)
.show();
}
});
}
});
As Stack Trace suggested use addOnSuccessListener instead of addOnCompleteListener like below:
auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
#Override
public void onSuccess(AuthResult authResult) {
//Save user to db
....
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
....
}
});
I created an app and I want to store some images in a Firebase Storage, this is working fine, those images are uploaded, but I want to save their path in the database and those values are not inserted.
Let me show you what I did:
I'm not showing you the entire function. Only the code that points to my problem.
List<String> picturesUrls = new ArrayList<String>(); //This is declared globaly
buttonSigningUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
uploadFile(mProfilePic,"profilePicture");
uploadFile(mIdPhoto,"idPicture");
uploadFile(mCriminalRecord,"criminalRecordPicture");
final List<String> pictureUrls = picturesUrls;
mFirebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
final CarrierUser carrierUser;
carrierUser = new CarrierUser(
pictureUrls
);
FirebaseDatabase.getInstance().getReference("User")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(carrierUser).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful()) {
Toast.makeText(SignupCarrier.this,"Your request has been sent for approval", Toast.LENGTH_LONG).show();
sendEmail(email, fullname);
openLogin();
}
}
});
}
}
});
}
});
Upload File Function :
private void uploadFile(Uri path,String forWho) {
if(path != null) {
StorageReference fileReference = mStorageRef.child(editemail.getText().toString()+"-"+forWho+"-"+System.currentTimeMillis()+"."+getFileExtension(path));
mUploadTask = fileReference.putFile(path)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
picturesUrls.add(taskSnapshot.getUploadSessionUri().toString());
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(SignupCarrier.this, e.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
}
And the Object:
import java.util.List;
public class CarrierUser {
public List<String> picturesUrls;
public CarrierUser() {
}
public CarrierUser(List<String> picturesUrls) {
this.picturesUrls = picturesUrls;
}
}
I do have another values and fields, but they are inserted, only this list with paths is not. Maybe I did something wrong when I add a path to that list, but I can't figure out what exactly is wrong. Can you please help me out? Thank you very much.
How can I check whether my data has been successfully pushed to Firebase?
I want to push my data like the picture
To know when the write operation is completed or not, you need to use a complete listener. So let's say you want to know when the write operation for your id_data property is completed, please use the followig code:
addReference.child("DataInputManual").child(key).child("id_data").setValue(key)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
//Do what you need to do
} else {
Log.d(TAG, task.getException().getMessage());
}
}
});
You can also use a success listener like this:
addReference.child("DataInputManual").child(key).child("id_data").setValue(key)
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
//Do what you need to do
}
});
You can use below code to check success or failure of upload on firebase Storage.
final StorageReference photoRef = FirebaseStorage.getInstance().getReference()
.child("image_folder_on_firebase")
.child("image_name" + ".jpeg");
photoRef.putFile("local pic uri( Object of Uri)").addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
newPicFirebaseUri = taskSnapshot.getStorage().getDownloadUrl().getResult();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
}
});