Reputation: 467
Below I have a signup code. This comes from a previous question I had about adding data to a user's table upon account creation. The code is as follows:
signup(){
firebase.auth().createUserWithEmailAndPassword(useremail, userpassword)
.then(function(response) {
var updates = {};
updates['/users/' + response.uid] = { email: useremail, password: userpassword, net_worth : 5000, username : 'Roy' };
firebase.database().ref().update(updates);
});
}
To be clear, my schema should look something like:
email|''
password|''
username|'Roy'
net_worth|'5000'
Now I would like to retrieve this information and place it in my profile component, this time retrieving that data. At the moment what I have is the following:
Profile Component
export class ProfileComponent implements OnInit {
private usermail: string;
private net_worth: string;
constructor(){}
ngOnInit(){
this.usermail = '';
this.net_worth = '';
var user = firebase.auth().currentUser;
if (user != null) {this.usermail = user.email;}
}
}
This code works for giving me the user's email from authentication, but it won't work in returning the other information in my schema, like net_worth
and username
. I imagine because it is stored in the database table, not the authentication function.
How would I call the user-generated ID to get the details of the specified user? Specifically, their username
and net_worth
?
Upvotes: 0
Views: 1227
Reputation: 467
Your database structure is like this
Users
user-Id1
email
net_worth
username
After successful login, you can query your db based on the response returned from firebase. uid
, just like you can access email
, there is also displayName
which can take the place of username
for you. For more info on managing users read here
To query your db, for other info.
var user = firebase.auth().currentUser;
if (user) {
var getUserInfo = firebase.database().ref('users/' + user.uid);
getUserInfo.once('value', function(snapshot) {
console.log(snapshot.val()); //returns net_worth, etc
});
}
Upvotes: 4