微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Firebase Realtime数据库中的权限被拒绝-“ auth!= null”规则不适用

如何解决Firebase Realtime数据库中的权限被拒绝-“ auth!= null”规则不适用

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

{
  "rules": {
    ".read": "false",".write": "false"
  }
}

这将提供访问权限:

{
  "rules": {
    ".read": "true",".write": "true"
  }
}

我想授予任何经过身份验证的用户读取和写入数据库的权限,但这不起作用:

{
  "rules": {
    ".read": "auth != null",".write": "auth != null"
  }
}

权限被拒绝。 我知道身份验证过程正在运行,因为登录后可以看到当前用户。似乎创建的auth对象为null。 有人可以提供有关如何在auth对象上获得更多可见性的提示吗,或者即使有用户登录也为什么它为null?

编辑: 我有一个SignIn组件,像这样:

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;

我也有一个非常相似的SignUp组件,单击sign up按钮后,此函数将运行:

    ...
      const signUp = () => {
          auth
              .createuserWithEmailAndPassword(email,password)
              .then((res) => {
                  history.push('/main');
               })
          .catch((err) => {
              console.log(err);
          });
      };
    ...

当我单击logout按钮时,此函数将运行:

    ...
          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
        }
    });
});

因为我在控制台中记录了用户信息,并且登录后它不为null,所以我认为数据库规则应适用,但不适用于数据库规则。我不知道我在想什么。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。