在Firebase用户表中添加额外的详细信息

我正在开发一个firebase + angularjs应用程序,我正在使用简单的电子邮件和密码身份validation,它正常工作。

我只是想知道,如果我可以添加额外的用户数据,正在使用的Firebase的电子邮件+密码身份validation,就像我想添加帐单信息和有关用户的其他详细信息,而无需创build额外的节点/表上firebase存储这些额外的数据。

Firebase会将电子邮件/密码用户存储在不能直接访问的独立位置。 您无法在此位置展开数据。

由于许多应用程序开发人员想要在其应用程序代码中访问用户数据,因此通常将所有用户存储在应用程序数据库本身内的/users节点下。 缺点是你必须自己做这个。 但是,积极的一面是,如果你愿意,你可以存储任何额外的信息。

有关存储示例代码的用户数据 ,请参阅Firebase指南 。 从那里:

 var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com"); ref.onAuth(function(authData) { if (authData && isNewUser) { // save the user's profile into Firebase so we can list users, // use them in Security and Firebase Rules, and show profiles ref.child("users").child(authData.uid).set({ provider: authData.provider, name: getName(authData) }); } }); 

Firebase具有一组固定的用户属性,可以更新但不添加。

但是,使用JSON.stringify() and JSON.parse() ,可以借助序列化和反序列化来添加less量数据。

然后使用任何一个未使用的属性来存储string

在DisplayName或photoURL属性中。 请记住,可以添加的数据必须小,并以stringforms存储。

这只能在FIREBASE SDK中使用,而不能在下面说明

 var user = firebase.auth().currentUser; user.updateProfile({ displayName: "Jane Q. User", photoURL: "https://example.com/jane-q-user/profile.jpg" }).then(function() { // Update successful. }, function(error) { // An error happened. }); 

你可以在这里以string的forms在photoURL或者displaYNamevariables中存储更多的json数据。

我的答案不是angular度相关的,但我安静了一下,找出如何使用聚合物和聚合物 燃料,所以我添加这个答案,以帮助人们做得比我做的更快。

我不得不像Frank van Puffelen提到的那样给db添加一个单独的节点。

import:

 <link rel="import" href="../bower_components/polymerfire/firebase-app.html"> <link rel="import" href="../bower_components/polymerfire/firebase-auth.html"> <link rel="import" href="../bower_components/polymerfire/firebase-document.html"> 

然后将您的应用中的任意位置放置<firebase-app>组件:

 <firebase-app name="yourAppName" api-key= "{{yourApi}}" auth-domain= "{{yourAuthDomain}}" database-url= "{{yourDbUrl}}" > </firebase-app> 

之后,您将需要使用<firebase-auth>和< firebase-document>

模板:

 <firebase-auth id="auth" app-name="yourAppName" signed-in="{{signedIn}}" user="{{user}}"> </firebase-auth> <firebase-document id="document" app-name="yourAppName" path="{{usersPath}}" // eg "/users" data="{{userDocument}}"> </firebase-document> 

脚本:

 this._register = function(){ var formValid = this.querySelector('#register-form').validate(); var auth = this.querySelector('#auth'); if(formValid && this.passWordsIdentic){ //The actual registration auth.createUserWithEmailAndPassword(this.email, this.password).then(function(user){ console.log('auth user registration succes'); //Example values this.userDocument.uid = user.uid; this.userDocument.email = user.email; this.userDocument.firstName = this.firstName; this.userDocument.lastName = this.lastName; this.userDocument.userName = this.userName; this.$.document.save(this.usersPath).then(() => { console.log("custom user registration succes"); this.$.document.reset(); }); }.bind(this)).catch(function(error) { var errorCode = error.code; var errorMessage = error.message; console.log('error: ', errorCode); ); } } 

就是这样,你可能想看看这个优秀的google codelab ,这是一个很好的介绍与聚合物使用firebase。

注意:只有在使用Firebase Admin SDK的情况下,此方法才有效,并且您需要在服务器上拥有端点以pipe理自定义令牌

Firebase Admin SDK可以使用其他声明对象创build自定义令牌,该对象可以包含任意数据。 这对于存储一些用户相关的信息可能是有用的,比如用户是否是高级用户。

其他声明数据可以使用auth对象访问。

 var uid = "some-uid"; //this can be existing user UID var additionalClaims = { premiumAccount: true, some-user-property: 'some-value' }; admin.auth().createCustomToken(uid, additionalClaims) .then(function(customToken) { // Send token back to client }) .catch(function(error) { console.log("Error creating custom token:", error); }); 

Firebase安全规则中也可以使用additionalClaims

有关更多信息,请阅读Firebase自定义令牌

这是一个迅速的版本。 你的用户结构(“table”)就像

 --users: -------abc,d@email,com: ---------------email:abc.d@email.com ---------------name: userName etc. 

通过auth FIRAuth.auth()?.createUser您可以在数据库中设置用户,如下所示:

  let ref = FIRDatabase.database().reference() let rootChild = ref.child("users") let changedEmailChild = u.email?.lowercased().replacingOccurrences(of: ".", with: ",", options: .literal, range: nil) // Email doesn't support "," firebase doesn't support "." let userChild = rootChild.child(changedEmailChild!) userChild.child("email").setValue(u.email) userChild.child("name").setValue(signup.name) 

请注意,方法在v4.0.0中更改。 因此,您需要使用下面的代码来检索用户configuration文件:

 afAuth.authState.subscribe((user: firebase.User) => { this.displayName = user.displayName; this.email = user.email; this.photoURL = user.photoURL; });