[storage/unauthorized] 用户未被授权执行所需操作

huangapple go评论64阅读模式
英文:

NativeFirebaseError: [storage/unauthorized] User is not authorized to perform the desired action

问题

我在将图像上传到Firebase存储时遇到了问题。我正在使用React Native的@react-native-firebase/storage,安装和连接似乎都很正常,因为我可以很好地引用图像:

const ref = firebase.storage().ref('myImage.png');

问题明显是基于权限的,因为当我暂时移除Firebase控制台存储规则中的:

: if request.auth != null

这部分时,一切正常:

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);
    });

那么,如何进行身份验证以安全地使用putFile?

编辑:
我正在使用"@react-native-firebase/storage": "^6.2.0"。

提前感谢您的帮助!

英文:

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!

答案1

得分: 8

编辑 Firebase 存储规则以允许上传而无需安装 @react-native-firebase/auth

  1. 转到 Firebase 控制台
  2. 在侧边栏中,在“开发”下打开“存储”
  3. 打开“规则”选项卡
  4. 替换下面的规则
rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write
    }
  }
}
  1. 然后发布

完成

英文:

Edit Firebase storage rules to allow uploads without the need of installing @react-native-firebase/auth.

  1. Navigate to Firebase console
  2. In Sidebar under Develop open Storage
  3. Open Rules tab
  4. replace below rule
rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write
    }
  }
}
  1. And Publish

Done

答案2

得分: 7

这就是了,Frank!

我错误地将应用程序视为已通过GoogleService-Info.plist文件进行身份验证的用户,而不是需要对应用程序的用户进行身份验证。

我的修复步骤:

  1. 安装 @react-native-firebase/auth
  2. 进入 Firebase 控制台的身份验证部分,启用匿名登录方法
  3. 在 componentDidMount() 中调用 firebase.auth().signInAnonymously()

最后,终于能够成功提交 putFile 到存储!

非常感谢 @FrankvanPuffelen !!

英文:

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:

  1. install @react-native-firebase/auth

  2. go into the Authentication section of your firebase console and Enabling the Anonymous Signin method

  3. call firebase.auth().signInAnonymously() in componentDidMount()

    And...finally able submit the putFile to storage successfully!

Big thanks @FrankvanPuffelen !!

答案3

得分: 4

我是新手编程,但我遇到了同样的问题。
在我的情况下,匿名登录无法正常工作。
我不得不更改目录位置才能使其正常工作。

原始规则:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {  => 在这里更改
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

更改为:

rules_version = '2';
service firebase.storage {
  match /b/Your_APP_Name.appspot.com/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

请注意,我只翻译了代码部分,没有包含其他内容。

英文:

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;
    }
  }
}

答案4

得分: 1

我们可以以两种方式解决这个问题。要么我们在上传文件到数据库时需要对用户进行身份验证。要么,如果仅用于测试目的,我建议转到Firebase中的存储,点击规则,将"allow read, write: if request.auth != null"更改为"allow read, write: if request.auth == null"。我建议对用户进行身份验证以确保文件的安全性和保护。

希望你的问题得到解决。

英文:

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.

huangapple
  • 本文由 发表于 2020年1月3日 20:34:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定