2016-09-26 34 views
3

我在我的網站上做了一個auth,它工作正常,但我想處理錯誤,爲了測試我爲此目的使用firebase返回這個目的:「已經有一個帳戶存在相同的電子郵件地址但登錄憑據不同,請使用與此電子郵件地址相關的提供商登錄。「我試着寫一個RXjs觀察到這樣做,用下面的代碼:Angularfire2和Rxjs Observable沒有發射Catch和完整的方法

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

我的控制檯從來沒有顯示「錯誤」或「做」,所以任何我把ERR代碼中不起作用。

enter image description here

注:我知道是什麼原因導致火力點回報,我只是想知道如何對待它,爲什麼我寫的代碼沒有預期一樣,當沒有返回錯誤,該網站按預期工作具有相同的可觀察性。

我使用angularfire2:^ 2.0.0-beta.5與角2.0.0最終

編輯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的workarround:

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

你的意思是這行'this.af.auth.subscribe(auth => console.log(auth));'?但那不是你在做什麼。看看最底層:https://github.com/angular/angularfire2/blob/master/docs/5-user-authentication.md#cordova-case 它們綁定'.then()'和'.catch() '從auth()返回Promise。signInWithCredential(provider)'。這就是你可以捕捉錯誤的方法。 – martin

+0

這就是apache cordova的情況,如果我使用彈出方法,也應該工作,我使用重定向方法,因此頁面重定向到提供程序頁面失去內存上的所有變量/狀態(然後funcion被調用重定向),再次當它回到我的頁面,如果我打電話auth.login將再次重定向到提供商頁面在無限循環... – Hllink

+0

我可以捕獲這樣的錯誤,但我仍然得到error_handler中引發的異常。 js和訂閱者。JS - 他們似乎打破了程序的執行 - 任何方式來沉默/忽略這些錯誤? –

回答

1

調用的createUserlogin方法時出現的任何錯誤AngularFire2 auth觀察到通過返回的承諾報告,而不是通過可觀察的。

例如:

import { AngularFire } from 'angularfire2'; 

... 
class MyComponent { 

    constructor(af: AngularFire) { 

    af.auth.createUser({ 
     email: '[email protected]', 
     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'); }); 
    } 
} 
+0

錯誤是* not *通過observable報告的,所以觀察者的錯誤回調從不被調用。 – cartant

+0

刪除了我的評論,因爲他們可能會讓人混淆,謝謝cartant! – Hllink

1

我檢查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())不是處理錯誤狀態的地方。

+0

因此,即時通過重定向登錄,每次頁面加載時,如果我調用this.af.auth.login()它重定向頁面,並在提供者認證後它回到我的頁面,它會被再次調用以獲得狀態並通過重定向變成一個無限循環,有沒有其他的方式來獲得用戶登錄狀態或唯一的方法是使用彈出方法,而不是重定向? – Hllink

+0

當你使用Redirect而不是Popup時 - 如果你在做移動設備時你必須使用它,那麼你將不會捕捉到這個錯誤 –

相關問題