Reputation: 2553
I'm having problems uploading an image to Firebase Storage. I'm using React Native's @react-native-firebase/storage and the installation and connectivity seem to be fine because I'm able to reference images just fine:
const ref = firebase.storage().ref('myImage.png');
The problem is definitely permissions based because when I temporarily remove:
: if request.auth != null
from Firebase console Storage Rules:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
This worked just fine:
firebase.storage()
.ref('images/test.jpg')
.putFile(myImage[0].path)
.then((successCb) => {
console.log('successCb');
console.log(successCb);
})
.catch((failureCb) => {
console.log('failureCb');
console.log(failureCb);
});
So how do I authenticate to use putFile securely?
EDIT: I'm using "@react-native-firebase/storage": "^6.2.0"
Thanks in advance!
Upvotes: 12
Views: 26256
Reputation: 430
I was getting the same error and I resolved it using below solution.
Before:
match /b/{bucket}/o {
match /users/{userId}/videos/{videoId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
}
}
initially I was uploading videos only so there was only security rules for videos only but later I wanted to upload images as well. So you just need to change few lines in firebase storage security rules.
After:
match /b/{bucket}/o {
match /users/{userId}/videos/{videoId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
match /users/{userId}/images/{imageId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
}
}
I hope it will be helpful. Let me know If you need more help from me.
Thank you!!!
Upvotes: 0
Reputation: 51
I'm new in coding, but i face same problem. In my Situation, It did not work with Ananymous SignIn. I had to change the Directory location to make it work.
Original Rule:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o { => Change here
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
Change to:
rules_version = '2';
service firebase.storage {
match /b/Your_APP_Name.appspot.com/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
Upvotes: 5
Reputation: 11
we can over come this in two ways. either we need to authenticate the user while uploading the files to the database. or if it is only for the testing purpose i recommend to go to storage in firebase and click on rules and change "allow read, write: if request.auth != null" to "allow read, write: if request.auth == null". i recommend to autheticate the user for safe and secure files.
I hope your problem solved.
Upvotes: 1
Reputation: 3383
Edit Firebase storage rules to allow uploads without the need of installing @react-native-firebase/auth
.
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write
}
}
}
Done
Upvotes: 10
Reputation: 2553
That's it Frank!
I wrongly thought of the app as a user that was already authenticated through the GoogleService-Info.plist file and not that the user of the app needs to be authenticated.
My steps to fix:
install @react-native-firebase/auth
go into the Authentication section of your firebase console and Enabling the Anonymous Signin method
call firebase.auth().signInAnonymously() in componentDidMount()
And...finally able submit the putFile to storage successfully!
Big thanks @FrankvanPuffelen !!
Upvotes: 7