【问题标题】:Uncaught (in promise) FirebaseError: The caller does not have permission in firestore未捕获(承诺中)FirebaseError:调用者在firestore中没有权限
【发布时间】:2021-09-06 22:32:59
【问题描述】:

当我点击 google oauth 按钮时,它给出了这个错误:

  1. 未捕获(承诺中)FirebaseError:调用者没有权限,
  2. 未捕获(承诺中)FirebaseError:缺少权限或权限不足。

你能帮帮我吗?

我的 Firebase 规则:

rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
 match /{document=**} {
  allow read, write: if request.auth.uid != null;
  }
 }
}

我的谷歌身份验证功能:

const GoogleAuth = () => {
  const onSuccess = async (response) => {
    var uid = response.profileObj.googleId;
    var username = response.profileObj.name;
    var email = response.profileObj.email;

    if (
      firebase.firestore().collection("users").where("uid", "==", uid).get()
    ) {
      await firebase.firestore().collection("users").doc(uid).set({
        username: username,
        email: email,
        uid: uid,
      });
    } else {
      return null;
    }
  };

  const onFailure = (response) => {
    console.log(response);
  };

  return (
    <div>
      <GoogleLogin
        clientId="70189954299-t7a9i8u7lhqm20ji4d3oei88ihef7i5o.apps.googleusercontent.com"
        buttonText="Login"
        onSuccess={onSuccess}
        onFailure={onFailure}
        cookiePolicy={"single_host_origin"}
        isSignedIn={true}
      />
    </div>
  );
};

【问题讨论】:

    标签: javascript google-cloud-firestore oauth-2.0 firebase-authentication firebase-security


    【解决方案1】:

    您的代码正在使用 Google 登录,但尚未使用 Firebase 身份验证登录。只有在您将用户登录到 Firebase 后,您的安全规则中的 request.auth 变量才会被填充,因此目前这些安全规则会正确拒绝该请求。

    要使用来自 Google 的凭据登录 Firebase,请致电 firebase.auth().signInWithCredential(credential),如 Firebase 文档的 Advanced: Handle the sign-in flow manually 部分所示。

    或者,您可以使用Handle the sign-in flow with the Firebase SDK 中描述的更简单的流程,它还为您处理对 Google 登录 API 的调用。

    【讨论】:

    • 你好。感谢您的回答。我看了这个,它的工作,但我不明白,我没有在我的凭据页面中使用客户端 ID,为什么?
    • 我不确定我是否理解,但关键在这里:var uid = response.profileObj.googleId;response.profileObj.googleId 不是 Firebase UID,因此比较不会导致任何匹配。最容易看到这一点的是打印两个值,您会发现它们完全不同。
    【解决方案2】:

    实际上,我在 firebase 中更改了我的规则配置..它对我有用..

    我从 allow read, write: if false 更改为 allow read, write: if true

    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if true;
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-22
      • 1970-01-01
      • 2021-11-15
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 2021-08-30
      • 2023-01-28
      相关资源
      最近更新 更多