2014-02-19 57 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; 
});