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

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!



Solution 1:[1]

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 !!

Solution 2:[2]

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

Solution 3:[3]

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.

Solution 4:[4]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 U.A
Solution 3 Thimma Creations
Solution 4 Samer Hmouda