rules_version = '2'; service cloud.firestore { match /databases/{database}/documents {…
- User
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /users/{userId} { allow read, write: if request.auth != null && request.auth.uid == userId; allow update: if request.auth != null && (request.auth.uid == userId || request.resource.data.keys().hasAny(['following', 'followers'])); }
match /users/{document} { allow read: if true; allow write: if false; }match /posts/{userId}/{post} { // Allow anyone to read and write to the posts collection allow read, write: if true; }
match /chats/{chatId}/messages/{messageId} { allow read, write: if request.auth != null; }} }
const handleFinalisePost = async () => { try { // 1. Check if user is authenticated if (!firebaseAuth.currentUser) { // Log authentication failure console.log('User not authenticated. Unable to finalize post.'); // Handle unauthenticated user (e.g., redirect to login) return; } else { // Log authentication success console.log('User authenticated successfully.'); }
// 2. Upload Image to Firebase Storage const storageRef = ref(storage, `images/${Date.now()}`); // Unique filename // Handle both string and string[] cases for images let imageUri; if (typeof images === 'string') { imageUri = images; } else if (Array.isArray(images) && images.length > 0) { imageUri = images[0]; // Use the first image if it's an array } else { // Handle the case where images is not valid return; } const response = await fetch(imageUri); const blob = await response.blob(); await uploadBytes(storageRef, blob); // 3. Get Download URL for the uploaded image const downloadURL = await getDownloadURL(storageRef); // 4. Create post data with timestamp const postData = { caption: caption, imageUrl: downloadURL, timestamp: Date.now(), // Add timestamp userId: firebaseAuth.currentUser.uid, }; // 5. Add post to Firestore const userPostsRef = collection(db, `posts/${firebaseAuth.currentUser.uid}/userPosts`); const postDocRef = doc(userPostsRef); // Firestore will automatically generate a unique ID for the document await setDoc(postDocRef, postData); // 6. Navigate back or show success message navigation.goBack(); // Or show a success message } catch (error) { console.error('Error uploading post:', error); // Handle error appropriately (e.g., show an error message to the user) } };ERROR Error uploading post: [FirebaseError: Missing or insufficient permissions.]
- Assistant