Firestore:无法访问Cloud Firestore后端.连接失败1次



我有一个Firebase实时数据库,该数据库与React Native集成,我可以在其中创建用户。但我的问题是,当我尝试登录时,我会遇到一个错误。如果有任何不同,我会在IOS上运行我的应用程序。我遵循了以下指南:如何构建React Native应用程序并将其与Firebase集成。我使用电子邮件/密码方法登录。我使用Firebase版本9.6.4.

我已经查看了这个线程:无法访问Cloud Firestore后端。尝试了所有的解决方案,但对我来说都无效。

关于可能导致错误的原因以及如何解决问题,有什么建议吗?

错误消息:

[2022-021-29T10:35:40.257Z]@firebase/firestore:,firestore(9.6.4(:无法访问Cloud Firestore后端。连接失败1次。最近的错误:FirebaseError:[代码=权限被拒绝]:CloudFirestore API以前未在项目xxxx中使用过,或已禁用。如果您最近启用了此API,请等待几分钟传播到我们的系统并重试的操作。这通常表示您的设备没有正常的Internet连接目前。客户端将以脱机模式运行,直到能够成功连接到后端。

Firebaseconfig.ts文件:

import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';
const firebaseConfig = {
apiKey: 'XXXXXXXXX',
authDomain: 'XXXXXXXXX.firebaseapp.com',
databaseURL: 'https://XXXXXXXXX.firebasedatabase.app',
projectId: 'XXXXXXXXX',
storageBucket: 'gs://XXXXXXXXX.appspot.com',
messagingSenderId: 'XXXXXXXXX',
appId: 'XXXXXXXXX',
};
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
export { firebase };

登录屏幕.tsx

import React, { useState } from 'react'
import { Image, Text, TextInput, TouchableOpacity, View } from 'react-native'
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
import styles from './styles';
import { firebase } from '../../firebase/config';
export default function LoginScreen({navigation}) {
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const onFooterLinkPress = () => {
navigation.navigate('Registration')
}
const onLoginPress = () => {
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then((response) => {
const uid = response.user.uid
const usersRef = firebase.firestore().collection('users')
usersRef
.doc(uid)
.get()
.then(firestoreDocument => {
if (!firestoreDocument.exists) {
alert("User does not exist anymore.")
return;
}
const user = firestoreDocument.data()
navigation.navigate('Home', {user})
})
.catch(error => {
alert(error)
});
})
.catch(error => {
alert(error)
})
}
return (
<View style={styles.container}>
<KeyboardAwareScrollView
style={{ flex: 1, width: '100%' }}
keyboardShouldPersistTaps="always">
<Image
style={styles.logo}
source={require('../../assets/images/logo.png')}
/>
<TextInput
style={styles.input}
placeholder='E-mail'
placeholderTextColor="#aaaaaa"
onChangeText={(text) => setEmail(text)}
value={email}
underlineColorAndroid="transparent"
autoCapitalize="none"
/>
<TextInput
style={styles.input}
placeholderTextColor="#aaaaaa"
secureTextEntry
placeholder='Password'
onChangeText={(text) => setPassword(text)}
value={password}
underlineColorAndroid="transparent"
autoCapitalize="none"
/>
<TouchableOpacity
style={styles.button}
onPress={() => onLoginPress()}>
<Text style={styles.buttonTitle}>Log in</Text>
</TouchableOpacity>
<View style={styles.footerView}>
<Text style={styles.footerText}>Don't have an account? <Text onPress={onFooterLinkPress} style={styles.footerLink}>Sign up</Text></Text>
</View>
</KeyboardAwareScrollView>
</View>
)
}

您的错误消息显示FirebaseError: [code=permission-denied]: Cloud Firestore API has not been used in project xxxx before or it is disabled.该应用程序在登录后尝试读取用户文档,但似乎无法访问。

在firebase控制台中检查你的firestore规则,看看read规则是否有效。Firestore可能已使用仅允许管理员用户读/写的规则进行初始化,或者使用仅允许在创建后一个月内进行所有读/写操作的规则进行了初始化。

为了让你的应用程序正常工作,你的firestore规则需要允许对用户集合进行读取访问。以下是你可以在你的消防仓库里设置的规则。你设置的规则越具体,应用程序就越安全。

允许对所有集合进行未经身份验证的访问

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

允许对所有集合进行身份验证访问

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

允许对用户集合进行身份验证访问

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

有关更多信息,请参阅消防商店安全文档。

最新更新