1
votes

Working with the firebase packages: 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:

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?

1
Please edit your question to include the relevant code that doesn't work the way you expect. Be sure to check logs and include error any relevant error messages. As your question stands now, there is not much we can do to help. The Firebase Storage SDK does not need to be told about Firebase Authentication - they work automatically together. - Doug Stevenson

1 Answers

1
votes

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