【发布时间】: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