ShinCat
ShinCat

Reputation: 139

FirebaseErrorr: Missing or insufficient permissions happens, When trying to get snapShot

I tried to get snapShot with React.js and Webpack, and had the error Uncaught (in promise) FirebaseError: Missing or insufficient permissions. Thank you very much in advance.

I suspected my webpack setting is something wrong to make the async request. However, when I tried the simple async request with the same setup, there is no issue. On top of this, Getting document also works correctly. I'm totally lost what I should do next.

//App.js

componentDidMount() {
    this.unsubscribeFromAuth = auth.onAuthStateChanged(async user => {
      this.setState({ user });
      await createUserProfDoc(user);
    });
  }

//FireBase code

export const firestore = firebase.firestore();
export const createUserProfDoc = async (userAuth, additionalData) => {
  if (!userAuth) {
    return;
  }
  const userRef = firestore.doc('users/123');
  const snapShot = await userRef.get();
  console.log(snapShot);
};

Upvotes: 1

Views: 2283

Answers (2)

Dalang
Dalang

Reputation: 31

Wrap it inside your auth function:

firebase.auth().onAuthStateChanged((user) => {
      firebase
        .firestore()
        .collection('test_data')
        .where('winner', '==', true)
        .get()
        .then((response) => {
          response.forEach((item) => {
            console.log(item.data())
          })
        })
    })

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

If the error is come from the code you shared, it looks like the user doesn't have permission to read document users/123.

More likely you want to read the user's profile document, which is usually defined/keyed on their UID. In code that's be:

const userRef = firestore.collection('users').doc(user.uid);

Upvotes: 2

Related Questions