I'm trying to update the Email details of the currently logged in user in Firebase, it's working correctly, but I'm wondering if there's a better way for me to detect a duplicate email error.
I think I have two options:
- Run a check to see if the email the user inputs is already registered in Firebase, and if not, then proceed with the function to insert it.
or
- Attempt to insert the email first, and then handle the error if it detects that the email already exists.
I think the second method would be more efficient, as most users won't enter a duplicate email, so running this check every time seems pointless. But what is the correct way to handle the detection of a duplicate email in the second method?
This is my current code:
function submitEmail(){
var requesteduseremail = "myemail@email.com";
var currentuser = firebase.auth().currentUser;
currentuser.updateEmail(requesteduseremail).then(function() {
console.log("Email should now be entered into the Firebase console.");
}, function(error) {
console.log("Something went wrong: "+error);
if(error == "Error: The email address is already in use by another account."){
console.log("That email is already registered... so handle it here.");
}
});
}
This code is working correctly, I can show a message to the user informing them that the email is already registered. But since I'm using the Firebase error text "Error: The email address is already in use by another account."
to detect this error, it will break if Firebase ever change the error message. Is there perhaps a numerical error code I could use instead? Something that would be less prone to being changed by Firebase into the future?
Thank you for your help.
UPDATE
Current attempt to check if the email already exists:
var useremail = testemail1@email.com;
firebase.database().ref().child("active_emails").orderByChild("email").equalTo(useremail).once("value", function(snapshot) {
var emailSnapshot = snapshot.val();
if (emailSnapshot){
console.log("Sorry, that email already exists");
}else{
console.log("Email doesn't already exist, can now run function to create the account.");
}
});
This keeps saying that the email doesn't exist, even when I try an email that is already registered with the Firebase app. I think I may be referencing the data incorrectly.
This is the layout of my database:
MYAPP
|_______chat
|
|_______users
| |_____userA
| | |______email
| | |______first_name
| | |______last_name
| |
| |_____userB
|
|_______active_emails
|_______"email":"testemail1@email.com"
|_______"email":"testemail2@email.com"
So I'm trying to check to see if the var useremail
is contained inside active_emails
I take it the error is in the line firebase.database().ref().child("active_emails").orderByChild("email").equalTo(useremail).once("value", function(snapshot){
but I haven't been able to figure it out.
Any help with this would be really appreciated, thanks again!
UPDATE 2
My current attempt:
firebase.database().ref('/active_emails/').equalTo(useremail).once("value", function(snapshot) {
var emailSnapshot = snapshot.val();
if(emailSnapshot){
console.log("Email exists.");
}else{
console.log("Email doesn't exist.");
}