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": "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": [""] } } }
and I have manually created two users for authentication:
The issue is when I log in as "Naseebullah" (naseebullah@kcl.ac.uk), 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": "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] },
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?
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.
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.
}
});
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.