firebase_rules 0.0.1+2 copy "firebase_rules: ^0.0.1+2" to clipboard
firebase_rules: ^0.0.1+2 copied to clipboard

A type-safe Firebase rules generator for Firestore, Storage, and Realtime Database

example/example.md

import 'package:firebase_rules/firebase.dart';
import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart';

bool isSignedIn(RulesRequest request) => request.auth != null;

bool isOwner(RulesRequest request, RulesString uid) {
  final requestingUid = request.auth?.uid;
  return requestingUid == uid;
}

@FirebaseRules(
  service: Service.firestore,
  functions: [
    isSignedIn,
    isOwner,
  ],
)
final firestoreRules = [
  Match<FirestorePath, FirestoreResource>(
    rules: (path, request, resource) => [
      Allow([Operation.read], request.auth?.uid == 'god'.rules),
    ],
    matches: (path, request, resource) => [
      Match<UsersPath, FirestoreResource<User>>(
        rules: (users, request, resource) => [
          Allow([Operation.read], isSignedIn(request)),
          Allow(
            [Operation.create, Operation.update],
            isOwner(request, users.userId.rules),
          ),
        ],
      ),
      Match<ContentPath, FirestoreResource<Content>>(
        rules: (content, request, resource) => [
          Allow(
            [Operation.read],
            isSignedIn(request) && resource.data.public,
          ),
          Allow(
            [Operation.write],
            rules.firestore
                    .get<User>(rules.path('/users/${request.auth?.uid}'.rules))
                    .contentIds
                    .rules
                    .contains(content.contentId) &&
                rules.firestore.exists(
                  rules.path(
                    '/users/${request.auth?.uid}'.rules,
                    database: 'default',
                  ),
                ),
          ),
        ],
      ),
    ],
  ),
];

abstract class User {
  List<String> get contentIds;
  Blob get profileImage;
}

abstract class UsersPath extends FirebasePath {
  String get userId;

  @override
  String get path => '/users/$userId';
}

abstract class Content {
  bool get public;
}

abstract class ContentPath extends FirebasePath {
  String get contentId;

  @override
  String get path => '/content/$contentId';
}

14
likes
0
pub points
58%
popularity

Publisher

verified publisheriodesignteam.com

A type-safe Firebase rules generator for Firestore, Storage, and Realtime Database

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

meta

More

Packages that depend on firebase_rules