0

There is a create user form in the front end where the details of the form field including email and password will go into the 'Users' collection and also create user with email-id and password in firebase authentication .It worked fine but since yesterday in my device only the authentication is getting created but the details are not getting stored in the collection and the same tried on other device neither authentication is created not its stored in firestore collection.

Is there problem in my code or the cloud firestore is facing such problem?....since it was working before 2 days.

Code:

 secondaryApp.auth().createUserWithEmailAndPassword(useremail , pswd).then(cred => {
        return database.collection('Users').doc(cred.user.uid).set({
            useremail : document.getElementById('useremail').value,
            firstname : document.getElementById('fname').value,
            lastname : document.getElementById('lname').value,
            userSkillarr : firebase.firestore.FieldValue.arrayUnion(...[userskillset,newskillId]),
            userRole : document.getElementById('userRole').value

       });
       
    })
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Are you getting any errors when creating a user? Have you tried to include a catch statement to collect error data? This snippet was taken from the [Firebase Doc](https://firebase.google.com/docs/auth/web/password-auth) `firebase.auth().createUserWithEmailAndPassword(email, password) .then((userCredential) => { // Signed in var user = userCredential.user; // ... }) .catch((error) => { var errorCode = error.code; var errorMessage = error.message; // .. }); ` – Hipster Cat Feb 25 '21 at 18:51
  • Yes I tried including catch statement but still it does not throw any error. I guess its something related to firebase-rules. – Dale Alphonso Feb 27 '21 at 11:51
  • @DiegoJ Initally the firestore rule was `rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if true; } } } ` and then firebase sent an alert after 2months from setting this rule "[Firebase] Your Cloud Firestore database has insecure rules" – Dale Alphonso Feb 27 '21 at 12:00
  • @DiegoJ also tried to modify the rules but nothing worked -- `rules `service cloud.firestore { match /databases/{database}/documents { //match logged in users doc in users collection match/Users/{userId}{allow create: if request.auth.uid != null; allow read,write: if request.auth.uid == userId;} match /Work/{workId} { allow read, write: if request.auth.uid != null; } match /Category/{categoryId} { allow read, write: if request.auth.uid != null;} match /Client/{ClientId} {allow read, write: if request.auth.uid != null;} } }` and yet its not working` – Dale Alphonso Feb 27 '21 at 12:03

1 Answers1

0

I didn't realize it until I saw it here but one part of the issue could lie within the createUserWithEmailAndPassword method it is asynchronous, most Firebase calls are Async, that means that your database might not be ready to receive cred.user.uid information by when you need it. here is more information on async calls on FireBase. This could well explain how it was working fine one day then it wasn't.

Anyway here's the code I replicated and worked :

    firebase.auth().createUserWithEmailAndPassword(useremail, pswd).then(function(cred) {

    db.collection('users').doc(cred.user.uid).set({
        useremail : document.getElementById('useremail').value,
        firstname : document.getElementById('fname').value,
        lastname : document.getElementById('lname').value,
        userRole : document.getElementById('userRole').value
    })
        .then((docRef) => {
        console.log("Document written with ID: ", cred.user.uid);
    })
        .catch((error) => {
        console.error(error);
    }); 

}, function(error) {

    // Handle Errors here. Consider documentation errors
    var errorCode = error.code;
    var errorMessage = error.message;
    console.error(error);
});

On that same note I did notice that it does sometimes not return an error message please try to use catch statements whenever possible in there you can manage exceptions properly and accordingly, like shown

You could also have repeated emails on a given error and not have know it, I recommend you look at this for more information on requirements for this method and try inputting simple examples that meet password requirements such as mail:yes@yes.com ; pass: nuf39wf

As for the rules you are mentioning, the rules you have now didn't allow me to create any users

I looked into this guide for testing rules,I ran proper tests using these rules:

rules_version = '2';
service cloud.firestore { match /databases/{database}/documents {
//match logged in users doc in users collection 
match/Users/{userId}
{allow create: if request.auth == null; allow read,write: if request.auth.uid == userId;} 
match /Work/{workId}
{ allow read, write: if request.auth.uid != null; } 
match /Category/{categoryId} 
{ allow read, write: if request.auth.uid != null;} 
match /Client/{ClientId} 
{allow read, write: if request.auth.uid != null;} } }  
Hipster Cat
  • 331
  • 2
  • 11