简体   繁体   English

Firebase / Angularfire 0.9 - 创建新用户和个人资料

[英]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. 要在Angularfire 0.9中创建新用户,我可以使用$ firebaseAuth服务和$ createUser方法。

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. 因此,如果我想向该用户添加配置文件,那么检索该用户Auth Data的最佳方法是什么,特别是“uid”,这样我就可以为我的新用户保存配置文件数据。 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. 在0.9.1中,uid将与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. 关键是将返回的promise(在我的示例中为authData )传递给后续函数。

In 0.9.0, it is necessary to call an authentication method in order to obtain the user's uid. 在0.9.0中,有必要调用身份验证方法以获取用户的uid。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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