javascriptfirebase-authenticationfirebase-storagereact-native-firebasereact-native-image-picker

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

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