我是编程和 Firebase 的新手,我使用 React Native 创建了一个待办事项应用程序,并成功连接了 Firebase 来存储待办事项,现在我添加了身份验证,以便我可以创建个人资料以将待办事项存储在我的帐户中。当我使用不同的电子邮件输入时,我仍然可以看到我使用以前的帐户创建的相同列表。
这是我要修改的firebase规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// This rule allows anyone with your Firestore database reference to view, edit,
// and delete all data in your Firestore database. It is useful for getting
// started, but it is configured to expire after 30 days because it
// leaves your app open to attackers. At that time, all client
// requests to your Firestore database will be denied.
//
// Make sure to write security rules for your app before that time, or else
// all client requests to your Firestore database will be denied until you Update
// your rules
match /{document=**} {
allow read, write: if request.time < timestamp.date(2023, 10, 29);
}
}
}
如有任何帮助,我们将不胜感激。
我尝试按照另一个页面中的建议添加此内容,但它破坏了我的应用程序
按照建议,我修改了问题,包括更多详细信息,以下是具有 Firebase 连接的两个组件:
这是List.tsx组件
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /todos/{userId}/{document=**} {
allow read, write: if request.auth.uid == userId;
}
}
}
const List = ({ navigation }: RouterProps) => {
const [todos, setTodos] = useState<any[]>([]);
const [todo, setTodo] = useState('');
useEffect(() => {
const todoRef = collection(FIRESTORE_DB, 'todos');
const subscriber = onSnapshot(todoRef, {
next: (snapshot) => {
const todos: Todo[]= [];
snapshot.docs.forEach((doc) => {
console.log(doc.data());
todos.push({
id: doc.id,
...doc.data(),
} as Todo);
});
setTodos(todos);
},
})
return () => subscriber();
}, []);
const addTodo = async () => {
const doc = await addDoc(collection(FIRESTORE_DB, 'todos'), { title: todo, done: false});
setTodo('');
}
const renderTodo = ({ item }: any) => {
const ref = doc(FIRESTORE_DB, `todos/${item.id}`);
const toggleDone = async() => {
updateDoc(ref, { done: !item.done })
}
const deleteTask = async() => {
deleteDoc(ref);
}
return (
<View style={styles.todoContainer}>
<TouchableOpacity onPress={toggleDone} style={styles.todo}>
{item.done && <Ionicons name='md-checkmark-circle' size={30} color='green' />}
{!item.done && <Entypo name='circle' size={30} color='black' />}
<Text style={styles.todoText}>{item.title}</Text>
</TouchableOpacity>
<Ionicons name='trash-bin-outline' size={30} color='red' onPress={deleteTask} />
</View>
)
}
return (.......rendering part)
}
这是 Login.tsx 组件
import { FIREBASE_AUTH } from '../../firebaseConfig';
import {
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
} from 'firebase/auth';
function Login() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [loading, setLoading] = useState(false);
const auth = FIREBASE_AUTH;
const signIn = async () => {
setLoading(true);
try {
const response = await signInWithEmailAndPassword(auth, email, password);
console.log('RESPONSE:', response);
} catch (error) {
console.log('ERROR:', error);
} finally {
setLoading(false);
}
};
const signUp = async () => {
setLoading(true);
try {
const response = await createUserWithEmailAndPassword(auth, email, password);
console.log('RESPONSE:', response);
alert('Welcome onboard!');
} catch (error) {
console.log('ERROR:', error);
} finally {
setLoading(false);
}
};
return (......rendering part)
}
不太确定该怎么做,正如我之前提到的,我是一般编程和 Firebase 的新手,提前感谢
在 Firestore 中,数据模型始终以集合开始,然后是该集合下的文档,然后在每个文档下可以有更多的子集合 - 每个子集合又包含文档。
您的规则中至少有一个问题是您在此处构建的路径:
match /todos/{userId}/{document=**} {
这条路径由三部分组成:
todo
、{userId}
,然后是{document=**}
,这意味着这似乎并不遵循我上面概述的模式。
您需要保持集合和文档的交替,例如:
match /users/{userId}/todos/{document} {