How Do I Use Javascript Promise Chaining With Firebase Authentication And Firebase?
I need to utilize a key stored in my firebase using the user's firebase user id. The steps to my function are the following: 1) Get the Firebase User ID after authentication 2)
Solution 1:
You must use firebase.auth().onAuthStateChanged
to get the uid bcs .auth.currentUser.uid
will NOT be available on page load. It shows up a microsecond after (it's asynchronous).
To do what you want simply build out a promise function and call it within .onAuthStateChanged
and .then
do your other function. You custom promise might look like:
functionfetchUserData(){
returnnewPromise(function(resolve, reject){
var db = firebase.firestore(); // OR realtime db
db.collection("users").get().then(function(snap) {
resolve (snap);
}).catch(function(error) {
reject (error);
});
});
}
Within .onAuthStateChange
just do something like:
fetchUserData().then((snap) => {
// do your thing
})
.catch((error) => {
console.log(error);
});
Post a Comment for "How Do I Use Javascript Promise Chaining With Firebase Authentication And Firebase?"