I am trying to fetch user info from user's database and access it from anywhere using rootscope.
I get the user's email and uid as the user signs in.
However,to get the user's info from database, I need to call my database and read the info one by one and store it as a variable in rootscope to access it from everywhere.
So, my question is below :
- How can I use rootscope in this example?
- Is there any better way to do this?
- in the below example, the console log is showing the first name, but I don't know how to rootscope it?
Thanks for help.
app.run(['$rootScope', '$state', '$stateParams', '$cookies', "$location",
function ($rootScope, $state, $stateParams, $cookies, $location) {
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
$rootScope.user.uid = user.uid;
$rootScope.user.email = user.email;
return firebase.database().ref('/users/' + user.uid).once('value').then(function(snapshot) {
var firstname = snapshot.val().firstname;
console.log("first name", firstname);
});
} else {
// No user is signed in.
}
});
}]);