0
votes

I want to make an app with angular and Firebase. I want to restrict read and write access for my database for one user. Therefore i made this rule.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if "auth.uid === 'gzL3tHnx6JQexMuK4h2Nyo0thHS2'"
    }
  }
}

This cause as expected an permission denied error.

The Framework makes it really simple to write and get data from the database, register users and make a login. Unfortunately the framework does not use HTTP Interceptors to append cred. to the request. Can somehow combine AngularFirestore and AngularFire Auth Methods. Example implementations below:

export class AppComponent {
  private shirtCollection: AngularFirestoreCollection<Shirt>;
  shirts: Observable<ShirtId[]>;
  constructor(private readonly afs: AngularFirestore) {
    this.shirtCollection = afs.collection<Shirt>('shirts');
    // .snapshotChanges() returns a DocumentChangeAction[], which contains
    // a lot of information about "what happened" with each change. If you want to
    // get the data and the id use the map operator.
    this.shirts = this.shirtCollection.snapshotChanges().pipe(
      map(actions => actions.map(a => {
        const data = a.payload.doc.data() as Shirt;
        const id = a.payload.doc.id;
        return { id, ...data };
      }))
    );
  }

private socialSignIn(provider: number): firebase.Promise<FirebaseAuthState> {
  return this.af.auth.login({AuthProviders.Github, method: AuthMethods.Popup})
    .then(() => this.updateUserData() )
    .catch(error => console.log(error));
}

}
1
Please limit yourself to a single question per post. I corrected your errors with the security rules in my answer. Consider splitting your second question into another post. - Doug Stevenson

1 Answers

0
votes

Your syntax isn't correct. Remove the quotes around the condition, change === to ==, add a semicolon, and refer to the uid with request.auth.uid:

allow read, write: if request.auth.uid == "gzL3tHnx6JQexMuK4h2Nyo0thHS2";

Please take a good look at the documentation for more examples.