2016-08-30 63 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 

我证实,新用户是在分贝...这是我的代码注册和登录

//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() Firebase 开始创建用户帐户。但是这可能需要一些时间,所以浏览器中的代码会继续执行。

立即继续使用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

相关问题