【发布时间】:2018-08-10 06:02:24
【问题描述】:
您好,我在寻找一种方法将身份验证状态更改自动定向到已登录用户的页面时遇到问题。但是,在创建用户之前,我想将用户配置文件推送到我的数据库。
所以我创建了用户,然后用这段代码添加到数据库中
firebase.auth().createUserWithEmailAndPassword(email, password).then(function (user){
firebase.database().ref('/Profiles').child(user.uid).set({
address: ''
})
}).catch(function(error){
let errorCode = error.code;
let errorMessage = error.message;
navigator.notification.alert('Error: Code: ' + errorCode + ', ' + errorMessage,false,'Error','Done');
});
但是,一旦 createUserWithEmailAndPassword 成功,此 onAuthStateChanged 函数会在添加数据库“配置文件”记录之前导航到新页面。
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
let u = firebase.auth().currentUser;
window.location = 'loggedIn.html';
}
});
如何让我的 onAuthStateChanged 函数在离开页面之前等待添加数据库记录
注意:: 我想保留 onAuthStateChanged,这样如果用户登录到会话,他们将自动定向到登录页面
【问题讨论】:
标签: javascript firebase firebase-authentication