Promises - Firebase Auth


#1

Dear friends, greetings to all!!

I’m Pedro, a new kind of programmer…

I’m trying to sync asyncronous callings to do a google authentication, but it’s not following the supposed order (and I’m getting crazy). It’s getting doing login, but not in the correct order…

Can anybody help me, pleeeeasee??

This is the main code:

this.authProvider.getGoogleLoginCredential()
  .then(() => {
    console.log("1: entering firebaseAuthentication");
    this.authProvider.firebaseAuthentication();
    console.log("3: exiting firebaseAuthentication");       
  }).then(() => {
    console.log("entering getUser().uid");
    this.authProvider.getUser().uid;
  }).then(() => {

Here authProvider where methods are called:

public firebaseAuthentication(): Promise {
return this.afAuth.auth.signInWithCredential(this.credential)
.then((success) => {
console.log('2: Firebase Authentication success: ’ + JSON.stringify(success));
})
}

getGoogleLoginCredential(): Promise {
return this.googlePlus.login({
webClientId:
xyxy.apps.googleusercontent.com
, offline: false
}).then(res => {
this.googleLoginData = (res);
console.log(this.googleLoginData);
this.credential = firebase.auth.GoogleAuthProvider.credential(
res.idToken
);
console.log(this.credential);
}
).catch(err => console.error(err));
}

And here is the result:
1: entering firebaseAuthentication
modal-login.ts:63
3: exiting firebaseAuthentication
modal-login.ts:65
entering getUser().uid

TypeError: Cannot read property ‘uid’ of null
at modal-login.ts:66
at t.invoke (polyfills.js:3)
at Object.onInvoke (core.es5.js:3890)

modal-login.ts:68
2: Firebase Authentication success: {“uid”:"RIvKWOi … }


#2

Wild guess: put return in the promise?

this.authProvider.getGoogleLoginCredential()
  .then(() => {
    console.log("1: entering firebaseAuthentication");
return    this.authProvider.firebaseAuthentication();

  }).then((value) => {
  console.log("3: exiting firebaseAuthentication",value);       

    console.log("entering getUser().uid");
 return    this.authProvider.getUser().uid; // is this a promise at all????
  }).then((vlaue) => {
value


#3

Tommertom… you are my hero!! It worked… Thank you very much!!!

About “this.authProvider.getUser().uid” // You are right, this is not a promise, I was only trying to understand what was happening.

All promisses need to have a return, isn’t it?

Thanks a lot


#4

In order to chain the promises like you were wanting you have to return a promise, yes.


#5

and don’t forget to add a .catch for any errors in the chain. Otherwise you will find yourself posting a question here regarding uncaught exception in promise…


#6

He he, I hadn’t forgot that :grin:
Thanks!!!