【问题标题】:Possible Unhandled Promise Rejection (id: 1):The caller does not have permission to execute the specified operation only on android可能的Unhandled Promise Rejection (id: 1):调用者没有权限只在android上执行指定的操作
【发布时间】:2022-01-06 05:03:52
【问题描述】:

我制作了一个 react-native 应用程序,一切都在 ios 上完美运行,直到我在 android 上运行它,我得到了这个错误

Possible Unhandled Promise Rejection (id: 1):
Error: [firestore/permission-denied] The caller does not have permission to execute the specified operation.
NativeFirebaseError: [firestore/permission-denied] The caller does not have permission to execute the specified operation.

我仍然可以得到数据,但我不知道为什么我无法登录,这里是代码:

export default function Login(props: LoginI) {
  const user = useRef<User>();

  const [userExist, setUserExist] = useState<boolean>(false);
  let ListUser: any[] = [];
  const token = useSelector(
    (item: RootState) => item.persistedReducer.firebase.token
  );
  const dispatch: AppDispatch = useDispatch();
  const {} = props;
  const addNew = () => {
    firestore()
      .collection("Users")
      .doc(user.current?.user?.email)
      .set({
        userInfo: { ...user.current },
        note: firebase.firestore.FieldValue.arrayUnion(),
      });
    // .then(() => console.log("success"));
  };
  const getUser = async () => {
    await firebase
      .firestore()
      .collection("Users")
      .get()
      .then((data) => {
        data.forEach((snapshot) => {
          ListUser.push(snapshot.id);
        });
      });
  };
  useEffect(() => {
    if (user.current) {
      getUser();
    }
  }, [ListUser]);
  async function signIn() {
    // Get the users ID token
    const userInfo = await GoogleSignin.signIn();
    user.current = userInfo;
    await getUser();
    console.log(ListUser.includes(user.current.user?.email));
    if (ListUser.includes(user.current.user?.email)) {
      dispatch(
        signedIn({ token: userInfo?.idToken, userInfomation: userInfo.user })
      );
    } else {
      addNew();
      dispatch(
        signedIn({ token: userInfo?.idToken, userInfomation: userInfo.user })
      );
    }
    // ListUser = ListUser.concat(user.current.user?.email);

    // console.log(ListUser.includes(user.current.user?.email));
    // addNew();
    dispatch(
      signedIn({ token: userInfo?.idToken, userInfomation: userInfo.user })
    );

    // Create a Google credential with the token
    const googleCredential = auth.GoogleAuthProvider.credential(
      userInfo.idToken
    );

    // Sign-in the user with the credential
    return auth().signInWithCredential(googleCredential);
  }

  return (
    <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
      <Button
        onPress={() => {
          signIn();
        }}
      >
        <Text style={{ color: "white" }}>Login</Text>
      </Button>
    </View>
  );
}

我仍然不知道它是如何工作的,之前我将 if request.auth != null; 更改为 if request.auth.uid != null; 这是firebase的规则:

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

现在它得到错误 id 1,这是怎么回事???????请帮忙,万分感谢

【问题讨论】:

    标签: javascript react-native google-cloud-firestore firebase-security


    【解决方案1】:

    您收到的“Permission denied”错误是由于您的数据库当前具有的规则造成的。

    当您希望仅允许经过身份验证以在您的数据库中执行操作的用户进行访问。规则应如下所示:

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

    但是,如果您需要一组更细粒度的规则,例如,只允许 UID 值等于来自身份验证过程的 UID 值的经过身份验证的用户被能够写入自己的文档,那么你应该考虑使用以下规则:

    rules_version = '2';
    service cloud.firestore {
        match /databases/{database}/documents {
          match /users/{uid} {
            allow create: if request.auth != null;
            allow read, update, delete: if request.auth != null && 
    request.auth.uid == uid;
         }
       }
      }
    

    您也可以参考这个documentation。或者,您也可以查看GitHub link

    【讨论】:

    • 如果我的回答有用,您可以接受✔ 并点赞? :) 随时提问。
    猜你喜欢
    • 2021-07-25
    • 2021-12-26
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 2020-07-13
    • 2019-06-26
    • 2021-11-24
    相关资源
    最近更新 更多