Reactjs Firebase实时数据库中的权限被拒绝-“;啊!=空";规则没有';不适用

Reactjs Firebase实时数据库中的权限被拒绝-“;啊!=空";规则没有';不适用,reactjs,firebase,firebase-realtime-database,firebase-security,Reactjs,Firebase,Firebase Realtime Database,Firebase Security,我有一个带有Firebase身份验证和实时数据库的React应用程序。只要将实时数据库数据规则设置为true或false,它们就可以正常工作。因此,这会导致权限被拒绝: { "rules": { ".read": "false", ".write": "false" } } 这样就可以访问: { "rules": { ".read

我有一个带有Firebase身份验证和实时数据库的React应用程序。只要将实时数据库数据规则设置为true或false,它们就可以正常工作。因此,这会导致权限被拒绝:

{
  "rules": {
    ".read": "false",
    ".write": "false"
  }
}
这样就可以访问:

{
  "rules": {
    ".read": "true",
    ".write": "true"
  }
}
我想授予任何经过身份验证的用户在数据库中读写的权限,但这不起作用:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}
拒绝许可。 我知道身份验证过程正在运行,因为当我登录时,我可以看到当前用户。创建的
auth
对象似乎为空。 有人能提供一些提示,说明如何在
auth
对象上获得更高的可见性,或者为什么即使有用户登录,该对象仍为空

编辑: 我有一个
sign
组件,如下所示:

import React, { useState } from 'react';
import { useHistory } from 'react-router-dom';
import { auth } from '../firebase/firebaseConfig';

const SignIn = () => {
    const history = useHistory();
    const [email, setEmail] = useState('');
    onst [password, setPassword] = useState('');

    const signIn = () => {
        auth
        .signInWithEmailAndPassword(email, password)
        .then((res) => {
            history.push('/home');
            console.log(res.user);
        })
        .catch((err) => {
            console.log(err);
        });
    };

  return (
      <div className="signIn">
          <h1 className="signIn-title">Sign in to your account</h1>
          <input
              type="text"
              placeholder="Enter your email"
              value={email}
              onChange={(e) => setEmail(e.currentTarget.value)}
          />
          <input
              type="password"
              placeholder="Enter your password"
              value={password}
              onChange={(e) => setPassword(e.currentTarget.value)}
          />
      <button onClick={signIn}>Sign In</button>
      </div>
   );
};

export default SignIn;
当我点击
注销
按钮时,此功能运行:

    ...
      const signUp = () => {
          auth
              .createUserWithEmailAndPassword(email, password)
              .then((res) => {
                  history.push('/main');
               })
          .catch((err) => {
              console.log(err);
          });
      };
    ...
    ...
          const logOut = () => {
              auth
                  .signOut()
                  .then((res) => {
                      history.push('/');
                      console.log(res.user);
                   })
              .catch((err) => {
                  console.log(err);
              });
          };
    ...
我在这个SPA中有一些链接是受限制的,所以当我注销时,我肯定无法访问它们。 渲染这些视图时,将在useEffect挂钩中运行:

useEffect(() => {
    auth.onAuthStateChanged((user) => {
        console.log(auth.currentUser); // this is not null (shows the user) when logged in, null when logged out
        if (user) {
            history.push('/home');
            console.log(user.email); // this shows the user's email in all components
        }
    });
});

因为我在控制台中记录用户信息,并且在我登录时它不是空的,所以我假设数据库规则应该适用,但它们不适用。我不知道我遗漏了什么。

这些规则很好,而且很有效。这将把问题缩小到您的代码——您如何知道用户已通过身份验证?您正在签入代码吗?如果是的话,你能把代码包括进来让我们看一下吗?我已经添加了一些代码和更多信息。你有尝试编写的代码吗?在该代码中,就在读/写之前,您可以输出到控制台当前已验证的用户,以确保用户对象实际上已验证?谢谢Jay,问题在于向数据库发出请求的代码。我将请求发送到错误的URL,请求未通过身份验证,因为我没有包含访问令牌。