[英]Flutter & Firebase - using firebase_storage along with firebase_auth
Working with the firebase packages: https://pub.dartlang.org/packages/firebase_auth https://pub.dartlang.org/packages/firebase_storage 使用Firebase软件包: https : //pub.dartlang.org/packages/firebase_auth https://pub.dartlang.org/packages/firebase_storage
I can sign into firebase using firebase_auth, and everything works great there, but when I use the firebase_storage package to upload a file I get access denied and I have to go into firebase and set the permissions to: 我可以使用firebase_auth登录到firebase,并且在那里一切正常,但是当我使用firebase_storage包上传文件时,访问被拒绝,我必须进入firebase并将权限设置为:
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;//: if request.auth != null;
}
}
}
This is not ideal for an application that uses authentication, how do I tell the firebase_storage package that I am authenticated via the firebase_auth package? 对于使用身份验证的应用程序来说,这不是理想的选择,我如何告诉firebase_storage软件包已通过firebase_auth软件包进行了身份验证?
You should not have that problem, I share a code that works for me. 您应该没有那个问题,我共享一个对我有用的代码。
Future<Null> ensureLoggedIn() async {
FirebaseUser firebaseUser = await auth.currentUser();
assert(firebaseUser != null);
assert(!firebaseUser.isAnonymous);
}
Future<String> uploadFile(File file) async {
assert(file != null);
await ensureLoggedIn();
int random = new Random().nextInt(100000);
String _instance = '/files/image_$random.jpg';
StorageReference ref = FirebaseStorage.instance.ref().child(_instance);
StorageUploadTask uploadTask = ref.putFile(file);
final Uri downloadUrl = (await uploadTask.future).downloadUrl;
return downloadUrl.toString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.