简体   繁体   中英

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 of the new users credentials. So if I want to add profile to that user what is the best way to retrieve that users 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);

});

In 0.9.1, the uid will be returned with the 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);
};

That should get you going. The key is to pass the returned promise ( authData in my example) to the follow-on function.

In 0.9.0, it is necessary to call an authentication method in order to obtain the user's uid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM