如何将每个用户链接到他们在 Firebase 中的数据? [英] How do I link each user to their data in Firebase?

查看:31
本文介绍了如何将每个用户链接到他们在 Firebase 中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划创建一个名为 users 的主树,其中将包含用作用户名的不同用户的名称.因此,每个用户名都将包含他们的数据,例如全名、地址、电话号码

I plan to create a main tree named 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.

我想知道如何在每个用户登录他们的个人资料时获取他们的数据.

I want to know how to get each user's data when they log in on their profile.

推荐答案

首先,我建议您花一些时间通过阅读 Firebase 指南(旧版 Firebase 指南的链接).那里提供了回答您自己的问题所需的一切信息.但为了简单起见,我将在这里举一个例子:

First of all i suggest you spend some time getting familiar with firebase by reading the Firebase Guide (Link to old Firebase Guide). Everything you need to know to answer your own question is available there. But for simplicity i will put an example here:

让我们从安全开始,以下是此示例所需的基本 Firebase 规则:(来源:了解安全)(旧来源:了解安全)

Lets start with security, here are the basic firebase rules you need for this example: (source: Understanding Security) (old 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 retrieving user data.

存储数据:(来源:Firebase 身份验证)(旧来源:用户身份验证)

Storing data: (source: Firebase Authentication) (old source: User Authentication)

// Get a reference to the database service
var database = firebase.database();
// save the user's profile into Firebase so we can list users,
// use them in Security and Firebase Rules, and show profiles
function writeUserData(userId, name, email, imageUrl) {
  firebase.database().ref('users/' + userId).set({
    username: name,
    email: email
    //some more user data
  });
}

生成的 Firebase 数据如下所示:

The resulting firebase data will look like this:

{
  "users": {
    "simplelogin:213": {
      "username": "password",
      "email": "bobtony"
    },
    "twitter:123": {
      "username": "twitter",
      "email": "Andrew Lee"
    },
    "facebook:456": {
      "username": "facebook",
      "email": "James Tamplin"
    }
  }
}

最后但并非最不重要的是检索数据,这可以通过多种方式完成,但对于本示例,我将使用 firebase 指南中的一个简单示例:(来源:读取和写入数据)(旧来源:检索数据)

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: Read and Write data) (old source: Retreiving Data)

//Get the current userID
var userId = firebase.auth().currentUser.uid;
//Get the user data
return firebase.database().ref('/users/' + userId).once('value').then(function(snapshot) {
    //Do something with your user data located in snapshot
});

添加返回数据示例

因此,当您以用户 twitter:123 登录时,您将获得对基于您的用户 ID 的位置的引用,并将获得以下数据:

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": {
          "username": "twitter",
          "email": "Andrew Lee"
        }

这篇关于如何将每个用户链接到他们在 Firebase 中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆