chuck lassiter
chuck lassiter

Reputation: 19

Firebase Angularfire authentication. Create user works but user data (email address) is not stored.

I've been struggling with this problem for a couple of days, I am able to create a user but I cannot save their email address under a users node.

 register: function(user) {
      return auth.$createUser({
        email: user.email,
        password: user.password
      }).then(function(regUser) {
        var ref = new Firebase(FIREBASE_URL+'users');


       var userInfo = {
        key         : regUser.uid, // ex: simplelogin:29
        date        : Firebase.ServerValue.TIMESTAMP,
        email       : user.email,
    }; // user info

    ref.child(users).set(userInfo);
});

Upvotes: 1

Views: 600

Answers (1)

André Kool
André Kool

Reputation: 4978

Here are a couple issues i see:

  • No specific shild node for each user. You are trying to save the userInfo directly in the users node instead of making a child node under users.
  • Firebase rules. Without knowing the rules you use i can't know for sure if this applies but can everyone write to that specific node or do you have to be logged in first?
  • Making the reference. Without knowing what FIREBASE_URL exactly is i can't tell if it's an isuue but if there isn't a / at the end doing + 'users' will give a wrong reference. I suggest using child as Frank also commented.

Resulting code would be something like this (don't forget to check your firebase rules):

register: function(user) {
    return auth.$createUser({
        email: user.email,
        password: user.password
      }, function(error) {
        if (error) {
          //catch possible errors
        } else {
          //no error, user has been created
          //start with logging user in to have firebase write acces
          auth.$authWithPassword({
              email: user.email,
              password: user.password
            }, function(error, authData) {
              if (error === null) {
                //no error so log in succesfull
                //Making the firebase reference
                var ref = new Firebase(FIREBASE_URL).child("users");
                //Get user id
                var uid = authData.uid;
                //Set data in firebase making a child using the user id 
                ref.child(uid).set({
                  key: regUser.uid, // ex: simplelogin:29
                  date: Firebase.ServerValue.TIMESTAMP,
                  email: user.email,
                });
              });
          }
        )};
    }

Upvotes: 1

Related Questions