check if a user exists in firebase

1

I am working on a project in ionic and I have done the login with googleAuthentication , when I login / register I add some data to my firestore , like the image and the email. In my mobile application I allow to change the image, which it does with respect to the data of the firestore database. The problem is that the logueo at firestore I have done with a set, so every time I login, I overwrite the data.

I would like to know if there is any possibility to check if the document already exists (the document is equal to the user's id, so simply passing the user's id would already be) or check if the user is already authenticated. I've tried it in different ways but I'm pretty new with this and I do not know how to deal with the data to know when it's empty and when it's not.

I leave part of my code in case you can help:

 addUserToBbdd(user){
    let coleccion = this.db.collection("users").doc(user.uid);
 
    this.db.collection('users').doc(user.uid).set({"email": user.email, "image": user.photoURL, "name": user.displayName}).then(newItem => {
      //el usuario no está en la base de datos y lo inserta.
    }).catch(err =>{
      //el usuario ya está en la base de datos.
    });
  }

I am using the latest version of Cordova, ionic 3 and angular 5 . the library for firestore is angularfire2/firestore.

Thanks in advance.

    
asked by user85355 08.05.2018 в 23:26
source

1 answer

1

Based on the code you have put to check if the user already exists, you should do the following:

var usuario = this.db.collection('users').doc(user.uid);
usuario.get().then( usuarioBBDD => {
                       if (!usuarioBBDD .exists) {
                             console.log("No existe el usuario");
                             //Entiendo que aqui va el código para asignar el mail e imagen
                       }
                       else{
                             console.log("Existe el usuario");
                       }
                    }

I hope it serves you. Greetings.

    
answered by 09.05.2018 в 09:23