2014-02-19 108 views
5

我正在用AngularFire創建一個新用戶。但是,當我簽署用戶時,我還要求提供名字和姓氏,並在註冊後添加該信息。Firebase/AngularFire創建用戶信息

$firebaseSimpleLogin(fbRef).$createUser($scope.signupData.email, $scope.signupData.password).then(function (user) { 
    // Add additional information for current user 
    $firebase(fbRef.child('users').child(user.id).child("name")).$set({ 
    first: $scope.signupData.first_name, 
    last: $scope.signupData.last_name 
    }).then(function() { 
    $rootScope.user = user; 
    }); 
}); 

上面的代碼工作時,它創建了Firebase(users/user.id/...)節點。

問題

當我使用新的用戶我得到了用戶的默認信息登錄:ID,電子郵件,UID等,但沒有名字。我如何將這些數據自動關聯給用戶?

回答

9

你不能。通過將登錄詳細信息存儲在自己的數據存儲中,Firebase隱藏了登錄管理的複雜性。這個過程對你的應用程序的僞造一無所知,這意味着它不知道你是否在或在哪裏存儲任何額外的用戶信息。它返回它所知道的作爲便利的數據(id,uid,email,md5_hash,provider,firebaseAuthToken)。

這取決於你的應用程序,然後採取[U] ID,並抓住你需要的任何應用程序特定的用戶信息(如名字,姓氏)。對於Angular應用程序,一旦獲得認證成功廣播,您就會希望擁有一個UserProfile服務,用於檢索您正在查找的數據。

而且,在你的代碼片段,考慮改變

.child(user.id) 

.child(user.uid) 

這會派上用場,如果你曾經支持後來的Facebook/Twitter的/假面認證。 uid看起來像「simplelogin:1」 - 它有助於避免不太可能,但可能發生供應商之間的ID衝突。

0

我對此有同樣的問題,覺得沒有人實際上有明確的答案(2年)。但是,下面是這種服務的外觀結構:

app.factory('Auth', function(FURL, $firebaseAuth, $firebaseObject, $rootScope, $window){ 
​ 
    var ref = new Firebase(FURL); 
    var auth = $firebaseAuth(ref); 
​ 
    var Auth = { 
    user: {}, 
​ 
    login: function(user){ 
     return auth.$authWithPassword({ 
     email: user.email, 
     password: user.password 
     }); 
    }, 
​ 
    signedIn: function(){ 
     return !!Auth.user.provider; 
    }, 
​ 
    logout: function(){ 
     return auth.$unauth; 
    } 
    }; 
​ 
    // When user auths, store auth data in the user object 
    auth.$onAuth(function(authData){ 
    if(authData){ 
     angular.copy(authData, Auth.user); 
     // Set the profile 

     Auth.user.profile = $firebaseObject(ref.child('profile').child(authData.uid)); 
     Auth.user.profile.$loaded().then(function(profile){ 
     $window.localStorage['gym-key'] = profile.gym.toString(); 
     }); 
    } else { 
     if(Auth.user && Auth.user.profile){ 
     Auth.user.profile.$destroy(); 
     } 
​ 
    } 
    }); 
​ 
    return Auth; 
});