can't update data in firestore even I set all rules to if true

235 views
Skip to first unread message

Joe

unread,
Dec 24, 2022, 1:27:50 PM12/24/22
to Firebase Google Group
Hi everyone, new to here

Now I'm working on a small project, what I want to do is to update user profile when they click the submit button. I'm using React, redux to do this
But I keep getting the Missing or insufficient permissions. so I tried to set every rule to if true, but I'm still getting them.
everyone can help me out?
Below is my rule
-----------------------------------------------------
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // Users
    match /users/{user} {
        allow read: if true;
        allow create: if true;
        allow update: if true;
    }
  }
}
-----------------------------------------------------
My code in redux slice
-----------------------------------------------------
export const updateUserProfile = createAsyncThunk(
  "user/updateUserProfile",
  async (formData, thunkAPI) => {
    const { firstName, lastName } = formData
    console.log(auth.currentUser)
    try {
      // update firebase auth
      if (firstName) {
        if (auth.currentUser.displayName !== `${firstName} ${lastName}`) {
          await updateProfile(auth.currentUser, {
            displayName: `${firstName} ${lastName}`,
          })
        }
      } else {
        if (auth.currentUser.displayName !== lastName) {
          await updateProfile(auth.currentUser, {
            displayName: lastName,
          })
        }
      }

      // update firestore
      let formCopy = { ...formData }
      formCopy.timestamp = serverTimestamp()
      delete formCopy.password
      const docRef = doc(db, "user", auth.currentUser.uid)
      console.log(auth.currentUser)
      await updateDoc(docRef, formCopy)
      delete formCopy.timestamp
      return formCopy
    } catch (err) {
      return thunkAPI.rejectWithValue(
        `Could not update the profile. ${err.message}`
      )
    }
  }
)
-----------------------------------------------------
-----------------------------------------------------
What i use for other project like below, this work perfectly
-----------------------------------------------------
-----------------------------------------------------
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // Users
   match /users/{user} {
        allow read;
        allow create;
        allow update: if request.auth.uid == user
    }
  }
}
-----------------------------------------------------
the only difference between 2 project is I'm using redux now. But I also tried to move the update function out of redux slice, still can't solve the problem...

Kato Richardson

unread,
Dec 27, 2022, 4:22:22 PM12/27/22
to fireba...@googlegroups.com
Hi Joe,

The code references updateDoc() and updateProfile() but neither of those are defined in your example code. It's also a bit complex for a minimal repro; you could simplify it considerably before sharing.

The rules are fine. The potential reasons it might be failing are because you are writing to the wrong path or because the authentication token hasn't resolved before you begin the write. The second is the likely answer. You can isolate which one is the cause turning on debugging and viewing the logs.

☼, Kato



--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/6e851d68-7129-4b70-8ba6-2998fc59ceb7n%40googlegroups.com.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages