web-dev-qa-db-ja.com

javascript firebase.auth()。signInWithEmailAndPasswordは成功イベントを取得できません

アプリで_Java-script_ _fire-base_を使用しています。

アプリでcreateUserWithEmailAndPasswordを正常に処理できます。また、_Fire-base Console_内のデータを示しています。

これがスニペットです。

_firebase.auth().createUserWithEmailAndPassword("[email protected]", "*******").catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      console.log("Eroor Msg"  + errorMessage);

      // ...
    });
_

次に、firebase.auth().signInWithEmailAndPasswordを実行しました。

上記の方法では、logへの成功イベントまたはメッセージを取得できません。 errorも投げていません。

これがスニペットです。

_firebase.auth().signInWithEmailAndPassword("[email protected]", "*****")
        .catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      if (errorCode === 'auth/wrong-password') {
        alert('Wrong password.');
      } else {
        alert(errorMessage);
      }
      console.log(error);
    });
_

私の質問は、このメソッドsignInWithEmailAndPasswordで成功イベントを取得する方法です。

どんな助けも大歓迎です。

10
Jay Rathod RJ

以下のコードを確認してください:

firebase.auth().signInWithEmailAndPassword("[email protected]", "******")
   .then(function(firebaseUser) {
       // Success 
   })
  .catch(function(error) {
       // Error Handling
  });
33
Libu Mathew

これはonAuthStateオブジェクトを使用して実行する必要があります。以下は、ユーザーが正常にログインしたかどうかを確認するコードです。

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

サインインに成功すると、このコードを実行できます。ところで、SignInWithEmailAndPasswordが検証するオブジェクトを返すことはありません。

3
Farrukh Faizy

戻りイベントで「関数」を使用するタイミングを人々に理解させるために、他のコードに少し批判的になることはできますか...

//run one function only
.then(function(firebaseUser) => this.myfunction(firebaseUser); );

//run lines of code
.then((firebaseUser) => {

        // line1
        // line2

);

Ionicの私のコードは、AngularCLIの場合は少し異なる場合があります

public async dologinUser(newEmail: string, newPassword: string): Promise<any> {
        try {
          return await this._afAuth.auth.signInWithEmailAndPassword(newEmail, newPassword)

            .then((firebaseUser) => {
              if (firebaseUser) {
                //do something
              }
            })

            .catch((error) => {

              var errorCode = error.code;
              var errorMessage = error.message;

              alert(errorMessage);


            });


        } catch (e) {
          console.error("big hu?", e);
        }

      }
0
PHILL BOOTH