要在 Angularfire 0.9 中创建新用户,我可以使用 $firebaseAuth 服务和 $createUser 方法。
但是据我所知,此方法不会返回任何新用户凭据。因此,如果我想向该用户添加配置文件,检索该用户身份验证数据(特别是“uid”)的最佳方式是什么,这样我就可以为我的新用户保存配置文件数据。下面是我正在尝试做的一个例子
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);
});
最佳答案
在 0.9.1 中,uid 将与 promise 一起返回。例如:
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);
};
这应该让你继续。关键是将返回的 promise (在我的示例中为 authData)传递给后续函数。
在0.9.0中,需要调用认证方法才能获取用户的uid。
关于javascript - Firebase/Angularfire 0.9 - 创建新用户和配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27221187/