Angularfire2和Rxjs Observable不触发Catch和Complete方法

时间:2016-09-26 18:38:30

标签: rxjs angularfire2

我在我的网站上做了一个auth,它工作正常,但我想处理错误,为了测试我为此目的导致了一个来自firebase的返回:“一个帐户已经存在,具有相同的电子邮件地址但不同的符号 - 在凭据中。使用与此电子邮件地址关联的提供商登录。“我试着写一个RXjs observable来做到这一点,使用以下代码:

private subscribleAuth() {
    this.af.auth
        .subscribe(
            auth => {
                this._auth = auth && auth.auth ? auth.auth : null;
                if (this._auth) {
                    this.loginState = 'logged';
                } else {
                    this.loginState = 'login';
                }

            },
            err => {
                console.log('error'),
                    this.loginState = 'error';
            },
            () => { console.log('done.') }
        );
}

我的控制台从不显示'错误'或'完成',所以我放入错误代码中的任何内容都不起作用。

enter image description here

注意:我知道导致firebase返回的原因,我只是想知道如何对待它以及为什么我写的代码没有按预期工作,当没有错误返回时,网站按预期使用相同的observable工作。 / p>

我正在使用angularfire2:^ 2.0.0-beta.5和Angular 2.0.0 final

编辑1:even the official github project tells you to get the user as an observable and subscrible to it on Override configuration / No config Example app section..

编辑2:reported as an issue也许这真的是一个错误,要保持这篇文章的更新。现在的解决方法是在github上使用 yashmurty 编写的代码:

import { AngularFire, AuthProviders } from 'angularfire2';
import { AuthBackend } from 'angularfire2/auth/auth_backend';

export class SocialmenuComponent {
    constructor(public af: AngularFire, private _authBackend: AuthBackend) {
        // this.af.auth.subscribe(
        this._authBackend.getRedirectResult().subscribe(
            (auth) => {
                if(auth) {
                  console.log(auth);
                } else {
                  console.log('User Not Logged In');
                }
            },
            (err) => {
                if(err) {
                  console.log('Error in auth.subscribe : ');
                  console.log(err);
                } else {
                  console.log('No Error detected in auth.subscribe');
                }
            },
            () => { console.log('done.') }
        );
    }
}

2 个答案:

答案 0 :(得分:1)

调用AngularFire2 createUser observable的loginauth方法时发生的任何错误都是通过返回的promises报告的,而不是通过observable报告的。

例如:

import { AngularFire } from 'angularfire2';

...
class MyComponent {

  constructor(af: AngularFire) {

    af.auth.createUser({
      email: 'someone@someplace.com',
      password: 'somepassword'
    })
    .then((authState) => { console.log('success'); })
    .catch((error) => { console.log('failure'); });
  }

如果使用涉及重定向的方法进行身份验证,您可以执行以下操作(jeffbcross solution from github):

import { FirebaseApp } from 'angularfire2';
import * as firebase from 'firebase';

...
class MyComponent {

  constructor(@Inject(FirebaseApp) fbApp: firebase.App) {

    fbApp.auth().getRedirectResult()
    .then((userCredential) => { console.log('success'); })
    .catch((error) => { console.log('failure'); });
  }
}

答案 1 :(得分:1)

我正在检查angular/angularfire2/blob/master/src/auth/auth.ts源代码,我发现它既不会调用error()也不调用complete(),所以行为是正确的。成功登录时只调用next()

这不是你应该如何使用AngularFire.auth。调用this.af.auth.login()会返回rejected on missing credentials one of these casesangular ui bootstrap doc内部,因此firebase.Promise<FirebaseAuthState>(调用Observer)不是处理错误状态的地方