如何将每个用户链接到Firebase中的数据? [英] How do I link each user to their data in Firebase?
问题描述
我打算创建一个主要的树型用户,这个用户将包含不同用户用作USERNAME的名字。所以每个用户名都会包含他们的数据全名,地址,电话号码,我想知道如何获取每个用户的数据时,他们的个人资料登录。首先,我建议你花一些时间熟悉Firebase,阅读 Firebase指南。所有你需要知道回答自己的问题在那里。但是为了简单起见,我将在这里举一个例子:
让我们从安全开始,下面是这个例子需要的基本的firebase规则:(source:了解安全性)
rules:{
users:{
$ user_id:{
.write:$ user_id === auth。 uid
}
}
}
我将跳过实际的用户创建和登录,并关注存储和检索用户数据的问题。
存储数据:(来源:用户身份验证)
//获取firebase参考
var ref = new Firebase(https://< YOUR-FIREBASE-APP> .firebaseio.com);
ref.onAuth(function(authData){
if(authData&& isNewUser){
//将用户的个人资料保存到Firebase中,以便我们列出用户
/ /在安全和Firebase规则中使用它们,并显示配置文件
ref.child(users)。child(authData.uid).set({
provider:authData.provider,
name :getName(authData)
//更多用户数据
});
}
});
产生的firebase数据如下所示:
{
users:{
simplelogin:213:{
provider:password,
名称:bobtony
},
twitter:123:{
provider:twitter,
name:Andrew Lee
$ {
provider:facebook,
name:James Tamplin
}
}
$最后但并非最不重要的数据的retreiving,这可以通过几种方式但是对于这个例子,我将使用一个来自Firebase指南的简单示例:(来源: Retreiving Data )
//获取正确的firebase参考
var ref = new Firebase(https:/ /<YOUR-FIREBASE-APP>.firebaseio.com\").child(\"users\").child(authData.uid);
//获取数据
ref.once(value,function(data){
//做一些东西,用户数据将在变量数据
} );
编辑:添加返回数据的示例
所以当你以twitter用户123登录时,你将会根据你的用户id得到一个对该位置的引用,并且会得到这个数据:
twitter:123:{
provider:twitter,
name:Andrew Lee
}
I plan to create a main tree call users which will include the name different users used as USERNAME. So from each username will be included their data e.g. Full Name, Address, Phone No. And I want to know how to get each user's data when the log in on their profile.
解决方案 First of all i suggest you spend some time getting familiar with firebase by reading the Firebase Guide. Everything you need to know to answer your own question is available there. But for simplicity i will put an example here:
Lets start with security, here are the basic firebase rules you need for this example: (source: Understanding Security)
{
"rules": {
"users": {
"$user_id": {
".write": "$user_id === auth.uid"
}
}
}
}
I will skip the actual user creation and logging in and focus on the question about storing and retreiving user data.
Storing data: (source: User Authentication)
//Get the firebase reference
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)
//some more user data
});
}
});
The resulting firebase data will look like this:
{
"users": {
"simplelogin:213": {
"provider": "password",
"name": "bobtony"
},
"twitter:123": {
"provider": "twitter",
"name": "Andrew Lee"
},
"facebook:456": {
"provider": "facebook",
"name": "James Tamplin"
}
}
}
And last but not least the retreiving of the data, this can be done in several ways but for this example i'm gonna use a simple example from the firebase guide: (source: Retreiving Data)
//Get the correct firebase reference
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com").child("users").child(authData.uid);
//Get the data
ref.once("value", function(data) {
// do some stuff once, user data will be in the variable data
});
EDIT: Added example of return data
So when you are logged in as user twitter:123 you will get a reference to the location based on your user id and will get this data:
"twitter:123": {
"provider": "twitter",
"name": "Andrew Lee"
}
这篇关于如何将每个用户链接到Firebase中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!