简体   繁体   English

Firebase createUserWithEmailAndPassword未在登录时授权用户

[英]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.

相关问题 Firebase 使用 createUserWithEmailAndPassword 后看不到用户 - Firebase cannot see user after using createUserWithEmailAndPassword 我正在尝试将用户信息添加到 firebase createuserwithemailandpassword - i am trying to add user information to firebase createuserwithemailandpassword 授权用户 - Authorizing the user 用于createUserWithEmailAndPassword的Firebase和Captcha - Firebase & Captcha for createUserWithEmailAndPassword Firebase createUserWithEmailAndPassword上的超时连接 - Timeout connection on firebase createUserWithEmailAndPassword 如何侦听由 firebase createUserWithEmailAndPassword 创建的用户已写入 firebase 数据库? - How to listen that a user created by firebase createUserWithEmailAndPassword has been written to the firebase database? Firebase Auth createUserWithEmailAndPassword 返回“没有与此标识符对应的用户记录。用户可能已被删除。” - Firebase Auth createUserWithEmailAndPassword returns "There is no user record corresponding to this identifier. The user may have been deleted." Firebase onAuthStateChanged干扰createUserWithEmailAndPassword.then - Firebase onAuthStateChanged interfers createUserWithEmailAndPassword.then Firebase 身份验证为 createUserWithEmailAndPassword 返回未定义 - Firebase Auth returning undefined for createUserWithEmailAndPassword 使用 React 在 Firebase 中的 createUserWithEmailAndPassword 上添加用户名 - Add username on createUserWithEmailAndPassword in Firebase with React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM