0

I use the user registration and login through the firebase authentication, The moment the user registers / login , I want to store the additional user information (such as FirstName, LastName, Gender and etc.) in my database.

Here is what I am doing to do so but what happens when a rest call to store the new user information fails. The second time he logins he is not a new user

  loginWithFacebook() {
      const provider = new firebase.auth.FacebookAuthProvider();
      provider.addScope('user_birthday');
      provider.addScope('user_friends');
      provider.addScope('user_gender');
      return new Promise<any>((resolve, reject) => {
        this.afAuth.auth
        .signInWithPopup(provider) // a call made to sign up via fb
        .then(res => {
          if (res) {
            resolve(res);
            if (res.additionalUserInfo.isNewUser) { // creatin profile only if he is a new user
              this.createProfile(res); // a call to store the response in the db
            }
            this.setTokenSession(res.credential.accessToken);
          }
        }, err => {
          console.log(err);
          reject(err);
        })
      })
    }

How to always ensure that the new user information is stored in my own db?

Karty
  • 1,329
  • 6
  • 21
  • 32
  • Have you considered using the `firebase-admin` package? You could pass your user's ID once he's logged in and then synchronise with you DB in the backend https://firebase.google.com/docs/auth/admin/manage-users – t3__rry Nov 12 '18 at 08:50

1 Answers1

0

I don't know angular, but I think that you need to use the user uid as a key in the firebase database. I think that this uid is generated when a user first logs into your app, and is kept through the entire life of your app.. So when a user logs in, you can get the uid, and check in the Firebase DB, if there is an entry with the uid..if not, it means that you have a new user, and you should create an entry in the DB.

More details here: https://firebase.google.com/docs/auth/web/manage-users.

If you want to see how to find UID : Is there any way to get Firebase Auth User UID?

Teshte
  • 624
  • 1
  • 7
  • 26