2
votes

I have manually imported JSON file for the database with the following users:

{
  "users": {
    "Naseebullah": {
      "name": "Naseebullah Ahmadi",
      "gender": "Male",
      "type": "Patient",
      "Doctors": ["Ernest"],
      "age": "20",
      "DOB": "02/06/1997",
      "address": "122 Atherstone Court",
      "contactNumber": "07473693312",
      "emailAddress": "[email protected]",
      "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": "[email protected]",
      "profilePicture": "../Images/profile.jpg",
      "ECG": [""],
      "HeartSound": [""]
    }
  }
}

and I have manually created two users for authentication: enter image description here

The issue is when I log in as "Naseebullah" ([email protected]), I don't receive his details:

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

In other words, I don't get his name, gender and so on...

This is where I authenticate:

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);
  });
};

How can I retrieve the user's details from the database when the user is authenticated with email and password?

3
it looks like email is common in your auth and database. so first get the email from auth and then query users who has the specific email and display the data.Hareesh
Are you implying that we make the email as a unique key for classifying individuals? I think that's unsafe, No? @Hareeshuser7947407
Is there a better and alternative way?user7947407
that is the only way i can think you can reach to current user right now.Hareesh

3 Answers

1
votes

The authentication subsystem, firebase.auth() is not where to store user profile information. firebase.auth() will create a firebase.auth().currentUser.uid unique identifier for each user and may provide a few details such as an email address, photoURL, etc.

Create a users collection and store each users profile data such as age in a document whos identifier is the firebase.auth() uid.

Use .onAuthStateChanged to detect user sign in state and to subscribe to (get) the users profile data from the data store.

0
votes

With your current database structure you can fetch the user data in your onLogin method by doing:

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

To get the current user data.

However you should not be using the users' first names as keys in the users node, what will happen when you have two users named Ernest? It will break (you will delete the first Ernest user). Consider instead using the auth uid as the key, which is guaranteed to be unique and bound the user.

0
votes

Directly from the firebase website:

 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