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

5

11

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!

Emileeemili answered 3/1, 2020 at 12:34 Comment(3)
So it sounds like the user isn't signed in. Did you sign the user in with Firebase Authentication? If so, what is the smallest standalone script with which you can reproduce the problem?Cantaloupe
Thanks, @FrankvanPuffelen! How do I sign in? I'm using the same react-native-firebase/ for analytics and its working fine. Do you have a good recommendation on a tool to spin up a react native app with third-party modules online? I'll try to recreate it there for you. I didn't change any defaults so I think a plain project with firebase installed should show the error.Emileeemili
"How do I sign in?" firebase.google.com/docs/auth or (since you're using react-native-firebase) rnfirebase.io/docs/v5.x.x/auth/getting-startedCantaloupe
E
7

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

Emileeemili answered 3/1, 2020 at 23:56 Comment(1)
In case anyone's looking, here's the link for v6 react-native-firebase/auth docs invertase.io/oss/react-native-firebase/v6/authEmileeemili
A
10

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

Atilt answered 14/11, 2020 at 12:24 Comment(1)
technically this works, but maybe not the safest rules to add to your project. Remember that anyone will be able to write to your storage (any type or size of file). For testing purposes this is fine but once you go live it is better to set more specific rules for the projectSproul
E
7

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

Emileeemili answered 3/1, 2020 at 23:56 Comment(1)
In case anyone's looking, here's the link for v6 react-native-firebase/auth docs invertase.io/oss/react-native-firebase/v6/authEmileeemili
M
5

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;
    }
  }
}
Matronna answered 20/4, 2022 at 11:21 Comment(0)
P
1

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.

Penton answered 26/4, 2021 at 12:21 Comment(0)
W
0

I was getting the same error and I resolved it using below solution.

Before:

  match /b/{bucket}/o {
    match /users/{userId}/videos/{videoId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}

initially I was uploading videos only so there was only security rules for videos only but later I wanted to upload images as well. So you just need to change few lines in firebase storage security rules.

After:

  match /b/{bucket}/o {
    match /users/{userId}/videos/{videoId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
    match /users/{userId}/images/{imageId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}

I hope it will be helpful. Let me know If you need more help from me.

Thank you!!!

Windsucking answered 15/7 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.