2016-08-30 4 views
1

私はfirebaseアプリケーションをmonaca CLIとOnsenUIに接続しています。私はユーザーを作成し、同じアクションでログインしようとしています。私は成功し、ユーザーを作成することができますが、私は、ログインすることはできません。私は私でそれらをログインするとFirebaseでユーザーにサインインするときに「auth/user-not-found」

auth/user-not-found 

There is no user record corresponding to this identifier. The User may have been deleted 

次のエラーを取得する私は、新しいユーザーことを確認しましたdb内にあります...

//signup function stuff 
var login = function() { 
    console.log('got to login stuff'); 
    var email = document.getElementById('username').value; 
    var password = document.getElementById('password').value; 

    //firebases authentication code 
    firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) { 
    // Handle Errors here. 
    var errorCode = error.code; 
    var errorMessage = error.message; 
    console.log('User did not sign up correctly'); 
    console.log(errorCode); 
    console.console.log(errorMessage); 
    }); 

    firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) { 
    console.log(error.code); 
    console.log(error.message); 
    }); 

    fn.load('home.html'); 


}; 
+0

ユーザーを作成すると、自動的にそのユーザーがログに記録されるため、別々にログオンする必要はありません。 –

答えて

4

あなたのフローには、いわゆる競合状態があります。

createUserWithEmailAndPassword()に電話すると、は、というユーザーアカウントを作成します。しかし、これには時間がかかることがあるので、ブラウザのコードは実行を継続します。

すぐにsignInWithEmailAndPassword()に続きます。 Firebaseはまだユーザアカウントを作成している可能性が高いので、この呼び出しは失敗します。このような状況で、一般的に

ソリューションはthen()と、たとえば、一緒にチェーンに呼び出しです:

firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) { 
    firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) { 
    console.log(error.code); 
    console.log(error.message); 
    }); 
}).catch(function(error) { 
    // Handle Errors here. 
    var errorCode = error.code; 
    var errorMessage = error.message; 
    console.log('User did not sign up correctly'); 
    console.log(errorCode); 
    console.console.log(errorMessage); 
}); 

しかし、アンドレ・クールは、すでにコメントのように:自動的にユーザーを作成することは、すでにそれらを署名し、そうこの場合にはあなただけ行うことができます。

firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) { 
    // User is created and signed in, do whatever is needed 
}).catch(function(error) { 
    // Handle Errors here. 
    var errorCode = error.code; 
    var errorMessage = error.message; 
    console.log('User did not sign up correctly'); 
    console.log(errorCode); 
    console.console.log(errorMessage); 
}); 

あなたはおそらくすぐにまた、彼らはあなたのページに到達したときにdetect whether the user is already signedしたいと思います。そのためにはonAuthStateChangedを使用してください。ドキュメントから:

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

偉大な答えをありがとう。私はあなたの変更を実装し、もはや問題に直面していません。私の次の質問は、後に.push()をしようとすると「許可が拒否されました」というエラーがどうして起こるかです。私のユーザーがサインインしていないかのように – IWI

関連する問題