Firebase / Angularfire 0.9 - Create New User and Profile

To create a new user in Angularfire 0.9, I can use the $ firebaseAuth service and the $ createUser method.

However, from what I can tell, this method does not return any new user credentials. So if I want to add a profile to this profile, the best way to get the Auth Data, specifically the "uid", so I can save the profile data for my new user. Below is an example of what I am trying to do

var FIREBASEURL = "https://<my-firebase>.firebaseio.com"

var ref = new Firebase(FIREBASEURL);
$rootScope.authObj = $firebaseAuth(ref);

var newUser = {
  email: "email@email.com",
  password: "password",
  displayName: "Display Name",
  favFood: "Food"
};

$rootScope.authObj.$createUser(newUser.email, newUser.password).then(function() {

  console.log("User created successfully!");

  // Retrieve new Auth Data specifically uid    

  var newAuthData = ??????????

  // Remove password from object and create user profile

  delete newUser.password;
  newUser.timestamp = Firebase.ServerValue.TIMESTAMP;

  var userRef = new Firebase(FIREBASEURL + '/users/');
  userRef.child( newAuthData.uid ).set(newUser);

}).catch(function(error) {

  console.error("Error: ", error);

});

      

+3


source to share


1 answer


In 0.9.1, the uid will be returned with a promise. For example:

var FIREBASEURL = "https://<my-firebase>.firebaseio.com"

var ref = new Firebase(FIREBASEURL);
$rootScope.authObj = $firebaseAuth(ref);

var newUser = {
  email: "email@email.com",
  password: "password",
  displayName: "Display Name",
  favFood: "Food"
};

$rootScope.authObj.$createUser(newUser.email, newUser.password).then(function(authData) {
  console.log(authData.uid); //should log new uid.
  return createProfile(newUser, authData);
});


function createProfile(authData, user){         
  var profileRef = $firebase(ref.child('profile'));
  return profileRef.$set(authData.uid, user);
};

      



This should get you going. The key is to pass the returned promise ( authData strong> in my example) to the subsequent function.

In 0.9.0, you need to call the authentication method to get the custom uid.

+7


source







All Articles