2017-01-17 8 views
0

私のアプリケーションで認証のためのプロバイダ - AuthHelperProviderがあります。ユーザーがログインに失敗したときは、ログインボタンを無効にする必要があります。この目的のために私は私のプロバイダでイベントを公開し、LoginPageコンポーネントにサブスクライブ:イオン2適切な方法イベントを使用する

AuthHelperProvider

this.http.get(query).map(res => <{error: string, clientId: string}>res.json()).subscribe(response => { 
     if (response.error == '') { 
     console.log(this.TAG + 'log in without errors'); 

     if (keepSingIn) { 
      this.storage.set(this.KEEP_LOGGED_IN, true); 
      this.setUsername(login); 
      this.setPassword(password); 
     } 

     this.setClientId(response.clientId); 
     this.events.publish('user:login'); 
     this.attemptCount = 0; 
     /*this.navCtrl.setRoot(TabsPage);*/ 
     } else { 
     console.log(this.TAG + 'log in with errors' + response.error); 
     this.events.publish('user:failedAttempt',++this.attemptCount); 
     this.errorHelper.showErrorAlert(response.error); 
     } 
    }); 

LoginPage

this.events.subscribe('user:failedAttempt', (attemptsCount) => { 
     console.log(this.TAG + 'number of failed attempts: ' + attemptsCount); 
     if (attemptsCount >= 3) { 
     this.isLoginBlocked = true; 
     this.errorAlert.showErrorToast('Number of attempts exceeded, try to restore your account'); 
     } else { 
     this.isLoginBlocked = false; 
     } 
    }); 

しかし、私は予期しない動作に会った:ハンドラをサブスクライブするためにattemptsCount回と呼ばれるイベント。それは馬鹿に見えますが、何が間違っていますか?

+1

をこんにちは、私はちょうど私の間違った答えを削除した、あなたはおそらくその答えにあなたの調査結果を追加する必要がありますし、スレッドがあるので、数日で、それを受け入れますQ&Aを完了してください。 –

答えて

0

私がこの問題を発見したのは、events.subscribe()メソッドの返された型の誤解でした。だから、引数の配列が返されると、それは次のようになります。

this.events.subscribe('user:failedAttempt', (attemptsCount) => { 
     console.log(this.TAG + 'number of failed attempts: ' + attemptsCount[0]); 
     if (attemptsCount[0] >= 3) { 
     this.isLoginBlocked = true; 
     this.errorAlert.showErrorToast('Number of attempts exceeded, try to restore your account'); 
     } else { 
     this.isLoginBlocked = false; 
     } 
    }); 
関連する問題