[英]NativeFirebaseError: [storage/unauthorized] User is not authorized to perform the desired action
I'm having problems uploading an image to Firebase Storage.我在将图像上传到 Firebase 存储时遇到问题。 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:我正在使用 React Native 的 @react-native-firebase/storage 并且安装和连接似乎很好,因为我能够很好地引用图像:
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:来自 Firebase 控制台存储规则:
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?那么我如何进行身份验证才能安全地使用 putFile 呢?
EDIT: I'm using "@react-native-firebase/storage": "^6.2.0"编辑:我正在使用“@react-native-firebase/storage”:“^6.2.0”
Thanks in advance!提前致谢!
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.我错误地认为该应用程序是已经通过 GoogleService-Info.plist 文件进行身份验证的用户,而不是该应用程序的用户需要进行身份验证。
My steps to fix:我的修复步骤:
install @react-native-firebase/auth安装@react-native-firebase/auth
go into the Authentication section of your firebase console and Enabling the Anonymous Signin method进入 Firebase 控制台的身份验证部分并启用匿名登录方法
call firebase.auth().signInAnonymously() in componentDidMount()在 componentDidMount() 中调用 firebase.auth().signInAnonymously()
And...finally able submit the putFile to storage successfully!并且...终于能够成功提交 putFile 到存储!
Big thanks @FrankvanPuffelen !!非常感谢@FrankvanPuffelen !!
I'm new in coding, but i face same problem.我是编码新手,但我面临同样的问题。 In my Situation, It did not work with Ananymous SignIn.在我的情况下,它不适用于 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;
}
}
}
Edit Firebase storage rules to allow uploads without the need of installing @react-native-firebase/auth
.编辑 Firebase 存储规则以允许上传而无需安装@react-native-firebase/auth
。
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write
}
}
}
Done完毕
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".或者,如果它仅用于测试目的,我建议转到 firebase 中的存储并单击规则并将“允许读取,写入:if request.auth != null”更改为“允许读取,写入:if request.auth ==空值”。 i recommend to autheticate the user for safe and secure files.我建议对用户进行身份验证以获得安全可靠的文件。
I hope your problem solved.我希望你的问题解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.