Firebase Firestore Rules: Permission denied when trying to update documents

I’m getting a “permission-denied” error when trying to update a document in Firestore. I’ve ensured that my Firestore rules allow the operation, but the update still fails.

Here are the relevant Firestore rules:

rules_version = '2';

service cloud.firestore {
  match /databases/{database}/documents {

    // Check if the user is an admin
    function isAdmin() {
      return request.auth != null &&
        get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 0;
    }

    // Check if the user is a business person
    function isBusinessPerson() {
      return request.auth != null &&
        get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 1;
    }

    // Rules for the clients collection
    match /clients/{clientId} {
      allow update: if isAdmin() || isBusinessPerson();
    }
  }
}

Here are the constants I use in the code:

const keyClient = 'clients';
const keyAddress = 'addresses';
const keyPhone = 'phone';
const keyName = 'name';

Here is the relevant update code:

Future<DataResult<ClientModel>> update(ClientModel client) async {
  try {
    if (client.id == null) {
      throw Exception('Client ID cannot be null for update operation.');
    }

    await _firebase.collection(keyClient).doc(client.id).update(client.toMap());

    return DataResult.success(client);
  } catch (err) {
    final message = 'ClientFirebaseRepository.update: $err';
    log(message);
    return DataResult.failure(FireStoreFailure(message: message));
  }
}

I’ve verified the following:

  • The create operation works as expected and follows the rules.
  • The allow update: if true; rule also doesn’t work.
  • I’ve logged locator<UserStore>().currentUser! and FirebaseAuth.instance.currentUser and they match.
  • Here is a sample of my log output for an admin user:
    [log] admin: 0
    [log] ClientFirebaseRepository.update: [cloud_firestore/permission-denied] The caller does not have permission to execute the specified operation.
    

I’m not sure what’s causing the update to fail. Any suggestions or insights would be greatly appreciated!

What I’ve tried:

  • Ensured that allow update: if true; is applied to see if it was a rule logic issue, but the update still fails.
  • Checked that create operations work fine, and removing permissions for create does indeed block the operation, so the rules are applied.
  • Verified that the user has an admin role (as shown in the log).
  • Logged both the UID from locator<UserStore>().currentUser! and FirebaseAuth.instance.currentUser. Both values match, so they are not out of sync.

Why am I getting a “permission denied” error when trying to update documents, even when the rules seem correct and allow updates? Is there something I’m missing with Firestore security rules or the way the update operation is performed in the code?

I was expecting that the update operation would work since the rules should allow the update for users with the admin role or business role.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật