[英]Firebase / Angularfire 0.9 - Create new user and profile
要在Angularfire 0.9中創建新用戶,我可以使用$ firebaseAuth服務和$ createUser方法。
但是,據我所知,此方法不會返回任何新用戶憑據。 因此,如果我想向該用戶添加配置文件,那么檢索該用戶Auth Data的最佳方法是什么,特別是“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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.