[英]Firebase createUserWithEmailAndPassword not authorizing user on sign in
I've created a system where a user signs up, and once they've successfully signed up, other information they've filled out in the form is supposed to get added to a node with their user ID in the database. 我创建了一个用户注册的系统,一旦他们成功注册,就应该将他们在表单中填写的其他信息与用户ID一起添加到数据库中。
In my createUserWithEmailAndPassword .then()
callback, I've added a function that is supposed to write to the database. 在我的createUserWithEmailAndPassword .then()
回调中,我添加了一个应该写入数据库的函数。 However, upon signing up, the database write always threw permission denied
errors. 但是,注册后,数据库写操作始终会引发permission denied
错误。 I decided to check the values in the firebase.auth().currentUser to make sure the new user was actually signed in, and they were. 我决定检查firebase.auth()。currentUser中的值,以确保新用户确实已经登录。
When I added the same database write function to the firebase sign-in callback, it worked. 当我将相同的数据库写入功能添加到firebase登录回调中时,它起作用了。
Here's my code; 这是我的代码; does anyone have any idea why the authentication isn't valid? 有谁知道为什么身份验证无效?
//Declared above the createUser function
function logUser(user, signUpPhoneNumber, department){
database.ref('users/' + user.uid).set({
phone: signUpPhoneNumber,
department: department
});
}
firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user){
console.log("Email: " +firebase.auth().currentUser.email) //this prints the correct currentUser information.
return user;
}).then(function(user){
console.log("Logged in: " + firebase.auth().currentUser.email); //this also prints correct user email info.
//signUpPhoneNumber and department declared elsewhere.
logUser(firebase.auth().currentUser.uid, signUpPhoneNumber, department);
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// [START_EXCLUDE]
if (errorCode == 'auth/weak-password') {
alert('The password is too weak.');
} else {
alert(errorMessage);
}
console.log(error);
// [END_EXCLUDE]
});
I understand that the create user function should automatically sign in the user, and the currentUser isn't null, yet somehow the permission keeps being denied (the database rules are the default rules of requiring authentication). 我知道create user函数应该自动登录用户,并且currentUser不为null,但是某种程度上,权限一直被拒绝(数据库规则是要求身份验证的默认规则)。
The security rules are: 安全规则是:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
I also tested the sign-up callback and onAuthStateChanged callback and found that the auth state change fires after the sign-up callback, yet putting the database write in the onAuthStateChanged callback doesn't work when triggered from a sign-up. 我还测试了注册回调和onAuthStateChanged回调,发现在注册回调后会触发auth状态更改,但是从注册触发时,将数据库写入onAuthStateChanged回调不起作用。 (It does work when triggered from a regular sign-in though). (不过,从常规登录触发后,它确实可以工作)。
The issue was with Firebase 4.5.2. 问题出在Firebase 4.5.2上。 Updating to 4.6.0 worked. 更新到4.6.0可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.