Ionic 3 / Angular

时间:2018-05-21 23:02:52

标签: ionic3 angular-promise

目前我有一个Firebase(Angularfire2)Auth提供程序,可以执行用户登录

signInWithEmail(credentials) { return this.afAuth.auth.signInWithEmailAndPassword(credentials.email, credentials.pw); }

我会在页面中调用该函数,如下所示:

login() { this.auth.signInWithEmail(credentials) .then( () => this.navCtrl.push(MainPage), (err) => { console.log("Return Code: " + err.code + " | Message: " + err.message); }); }

但是,现在我想按照以下方式嵌套承诺:

signInWithEmail(credentials) {
    firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
      .then(() => {
        return this.afAuth.auth.signInWithEmailAndPassword(credentials.email, credentials.pw);
      }, (err) => {
      return Promise.reject(err); 
    });
  }

这不起作用,因为返回void,因此无法再以当前方式调用signInWithEmail函数。

在返回signInWithEmailAndPassword Promise之前,实际处理setPersistence的Promise的最佳方法是什么?

我应该只在页面本身调用setPersistence方法而不是在提供程序中执行此操作吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

我通过在我的提供者中为第二个承诺创建另一个方法来解决它:

enablePersistance() {
   return firebase.auth().setPersistence(firebase.auth.Auth.Persistence.NONE);
}

在我的页面中链接承诺:

this.auth.enablePersistance().then(() => {
  this.auth.signInWithEmail(credentials)
    .then(
    () => {
      console.log("Success");
    },
    (err) => {
      console.log("Return Code Login: " + err.code + " | Message: " + err.message);
    });
  }, (err) => {
  console.log("Return Code Enable Persistance: " + err.code + " | Message: " + err.message);
});