9

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!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
sigmapi13
  • 2,355
  • 3
  • 30
  • 27
  • 1
    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](http://stackoverflow.com/help/mcve)? – Frank van Puffelen Jan 03 '20 at 15:40
  • 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. – sigmapi13 Jan 03 '20 at 16:45
  • 1
    "How do I sign in?" https://firebase.google.com/docs/auth or (since you're using `react-native-firebase`) https://rnfirebase.io/docs/v5.x.x/auth/getting-started – Frank van Puffelen Jan 03 '20 at 19:54

4 Answers4

8

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

U.A
  • 2,991
  • 3
  • 24
  • 36
  • 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 project – Sam Smets Apr 01 '22 at 06:23
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 !!

Community
  • 1
  • 1
sigmapi13
  • 2,355
  • 3
  • 30
  • 27
  • In case anyone's looking, here's the link for v6 react-native-firebase/auth docs https://invertase.io/oss/react-native-firebase/v6/auth – sigmapi13 Jan 04 '20 at 00:06
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;
    }
  }
}
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.