【问题标题】:Angularfire2 and Rxjs Observable Not firing Catch and Complete MethodsAngularfire2 和 Rxjs Observable 没有触发 Catch 和 Complete 方法
【发布时间】:2016-09-26 18:38:30
【问题描述】:

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

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.') }
        );
}

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

注意:我知道导致 firebase 返回的原因,我只是想知道如何处理它以及为什么我编写的代码没有按预期工作,当没有返回错误时,该站点在相同的 observable 下按预期工作。

我正在使用 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,也许这真的是一个错误,我会继续更新这篇文章。现在的解决方法是使用 yashmurty 在 github 上编写的代码:

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.') }
        );
    }
}

【问题讨论】:

  • 你是说这条线this.af.auth.subscribe(auth => console.log(auth));?但这不是你在做什么。查看最底部:github.com/angular/angularfire2/blob/master/docs/… 他们将.then().catch() 绑定在auth().signInWithCredential(provider) 返回的Promise 上。这就是您可以捕获错误的方式。
  • 这是 apache cordova 的情况,如果我使用 popup 方法也应该可以工作,我使用的是 redirect 方法,因此页面重定向到提供程序页面,丢失内存上的所有变量/状态(和then 函数在重定向之前被调用),当它返回我的页面时,如果我调用 auth.login 将再次在无限循环中重定向到提供程序页面...
  • 我可以捕捉到这样的错误,但我仍然在 error_handler.js 和 Subscriber.js 中抛出异常——它们似乎破坏了程序的执行——有什么方法可以使这些错误静音/忽略?
  • 我不知道,这只是一个解决方法,从现在开始我使用弹出方法,直到他们解决了问题。

标签: rxjs angularfire2


【解决方案1】:

调用AngularFire2 auth observable 的createUserlogin 方法时发生的任何错误都通过返回的promise 报告,而不是通过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'); });
  }
}

【讨论】:

  • 错误是通过observable报告的,所以观察者的错误回调永远不会被调用。
  • 删除了我的 cmets,因为它们会迷惑人们,谢谢 carant!
【解决方案2】:

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

这不是您应该使用AngularFire.auth 的方式。调用this.af.auth.login() 返回firebase.Promise<FirebaseAuthState>,即rejected on missing credentials 或内部one of these cases,因此Observer(调用subscribe())不是处理错误状态的地方。

【讨论】:

  • 所以,我使用重定向登录,每次页面加载时,如果我调用 this.af.auth.login() 它会重定向页面,并且在提供者身份验证后它返回到我的页面,它再次调用以获取状态并通过再次重定向变成无限循环,是否有任何其他方法可以获取用户登录状态或唯一的方法是使用弹出方法而不是重定向?
  • 当您使用 Redirect 而不是 Popup 时 - 如果您使用移动设备,您就必须使用它,那么您将无法发现该错误
猜你喜欢
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多