Angularfire2 和 Rxjs Observable 不触发 Catch 和 Complete 方法
Angularfire2 and Rxjs Observable Not firing Catch and Complete Methods
我在我的网站上做了一个授权,它工作正常,但我想处理错误,为了测试我为此目的用 firebase 的 return 造成了一个错误:"An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address."。我正在尝试使用以下代码编写一个 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.') }
);
}
我的控制台从不显示 'error' 或 'done',所以我放入错误代码中的任何内容都不起作用。
注意:我知道导致 firebase return 的原因,我只是想知道如何处理它以及为什么我编写的代码没有按预期工作,而没有错误 returned该网站按预期工作,具有相同的可观察性。
我正在使用 angularfire2: ^2.0.0-beta.5 和 Angular 2.0.0 final
编辑 2:reported as an issue 也许这真的是一个错误,我会保持这个 post 更新。目前的解决方法是使用 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.') }
);
}
}
调用 AngularFire2 auth
observable 的 createUser
或 login
方法时发生的任何错误都通过返回的承诺报告,而不是通过 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'); });
}
}
我正在检查 angular/angularfire2/blob/master/src/auth/auth.ts
source code and I see it never calls neither error()
nor complete()
so the behavior is correct. It calls just next()
是否成功登录。
这不是您应该使用的方式 AngularFire.auth
。调用 this.af.auth.login()
returns firebase.Promise<FirebaseAuthState>
即 rejected on missing credentials or internally in one of these cases 因此 Observer
(调用 subscribe()
)不是处理错误状态的地方。
我在我的网站上做了一个授权,它工作正常,但我想处理错误,为了测试我为此目的用 firebase 的 return 造成了一个错误:"An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address."。我正在尝试使用以下代码编写一个 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.') }
);
}
我的控制台从不显示 'error' 或 'done',所以我放入错误代码中的任何内容都不起作用。
注意:我知道导致 firebase return 的原因,我只是想知道如何处理它以及为什么我编写的代码没有按预期工作,而没有错误 returned该网站按预期工作,具有相同的可观察性。
我正在使用 angularfire2: ^2.0.0-beta.5 和 Angular 2.0.0 final
编辑 2:reported as an issue 也许这真的是一个错误,我会保持这个 post 更新。目前的解决方法是使用 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.') }
);
}
}
调用 AngularFire2 auth
observable 的 createUser
或 login
方法时发生的任何错误都通过返回的承诺报告,而不是通过 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'); });
}
}
我正在检查 angular/angularfire2/blob/master/src/auth/auth.ts
source code and I see it never calls neither error()
nor complete()
so the behavior is correct. It calls just next()
是否成功登录。
这不是您应该使用的方式 AngularFire.auth
。调用 this.af.auth.login()
returns firebase.Promise<FirebaseAuthState>
即 rejected on missing credentials or internally in one of these cases 因此 Observer
(调用 subscribe()
)不是处理错误状态的地方。