如何从反应原生的firebase auth中检索用户数据?

时间:2018-02-09 15:08:29

标签: javascript firebase react-native firebase-realtime-database firebase-authentication

我已经为以下用户手动导入了数据库的JSON文件:

{
  "users": {
    "Naseebullah": {
      "name": "Naseebullah Ahmadi",
      "gender": "Male",
      "type": "Patient",
      "Doctors": ["Ernest"],
      "age": "20",
      "DOB": "02/06/1997",
      "address": "122 Atherstone Court",
      "contactNumber": "07473693312",
      "emailAddress": "Naseebullah@kcl.ac.uk",
      "profilePicture": "../Images/profile.jpg",
      "ECG": [0, 0, 0, 0, 0.0000050048828125, 0.0000137939453125],
      "HeartSound": [0, 0, 0, 0, 0.0000050048828125, 0.0000137939453125]
    },

    "Ernest": {
      "name": "Ernest Kamavuako",
      "gender": "Male",
      "type": "Doctor",
      "Patients": ["Naseebullah"],
      "age": "30",
      "DOB": "20/12/1970",
      "address": "122 Harrow Street",
      "contactNumber": "07473033312",
      "emailAddress": "Ernest@kcl.ac.uk",
      "profilePicture": "../Images/profile.jpg",
      "ECG": [""],
      "HeartSound": [""]
    }
  }
}

我手动创建了两个用户进行身份验证: enter image description here

问题是当我以“Naseebullah”(naseebullah@kcl.ac.uk)登录时,我没有收到他的详细信息:

"Naseebullah": {
  "name": "Naseebullah Ahmadi",
  "gender": "Male",
  "type": "Patient",
  "Doctors": ["Ernest"],
  "age": "20",
  "DOB": "02/06/1997",
  "address": "122 Atherstone Court",
  "contactNumber": "07473693312",
  "emailAddress": "Naseebullah@kcl.ac.uk",
  "profilePicture": "../Images/profile.jpg",
  "ECG": [0, 0, 0, 0, 0.0000050048828125, 0.0000137939453125],
  "HeartSound": [0, 0, 0, 0, 0.0000050048828125, 0.0000137939453125]
},

换句话说,我没有得到他的名字,性别等......

这是我验证的地方:

const onLogin = creds => {
  const { email, password } = creds;
  //console.log("props123 ", values, nav);
  firebase.auth().signInWithEmailAndPassword(email, password).then((user) => {
    console.log("user: ", user);
    /*
     * User is logged in
     *
     * */
  }).catch(err => {
    const { code,message } = err;
    console.log("not loggedin, ", message);
  });
};

当用户通过电子邮件和密码进行身份验证时,如何从数据库中检索用户的详细信息?

3 个答案:

答案 0 :(得分:0)

使用您当前的数据库结构,您可以通过以下方式获取onLogin方法中的用户数据:

firebase
  .database()
  .ref('users')
  .orderByChild('emailAddress')
  .equalTo(email)
  .once('value', snap => console.log(snap.val()));

获取当前用户数据。

但是,您不应该将用户的名字用作users节点中的键,当您有两个名为Ernest的用户时会发生什么?它会中断(您将删除第一个Ernest用户)。考虑使用auth uid作为密钥,保证它是唯一的并绑定用户。

答案 1 :(得分:0)

直接从Firebase网站上获取:

 firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
   console.log("user: ", user);
   // User is signed in.
  } else {
   // No user is signed in.
  }
});

https://firebase.google.com/docs/auth/web/manage-users

答案 2 :(得分:0)

认证子系统firebase.auth()不在用户配置文件信息的存储位置。 firebase.auth()将为每个用户创建一个firebase.auth().currentUser.uid唯一标识符,并可能提供一些详细信息,例如电子邮件地址,photoURL等。

创建一个users集合,并将每个用户的个人资料(例如年龄)存储在标识符为firebase.auth() uid的文档中。

使用.onAuthStateChanged检测用户登录状态,并从数据存储预订(获取)用户个人资料数据。