Firestore rules work in simulator but failed when reading documents from angular app

Viewed 85

All rules are working fine in simulator but not through app. I have been following youtube channel Fireship's video on google signin with custom data angluar here.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}

My authservice

@Injectable({ providedIn: 'root' })
export class AuthService {

  user$: Observable<User>;

  constructor(
    private ngAuth: AngularFireAuth,
    private firestore: AngularFirestore,
  ) { 
    this.user$ = this.ngAuth.authState.pipe(
      switchMap(user => user ? this.userRef(user).valueChanges() : of(null)),
    );
  }

  private userRef({ uid }): AngularFirestoreDocument {
    return this.firestore.doc<User>(`users/${uid}`);
  }
}

Using in html

<img *ngIf="auth.user$ | async as user" [src]="user.photoURL" alt="user">

error details

2 Answers

You are passing user object to userRef. Try modifying the argument.

              :
      switchMap(user => user ? this.userRef(user.uid).valueChanges() : 
              :

  private userRef(uid: string): AngularFirestoreDocument {
    return this.firestore.doc<User>(`users/${uid}`);
  }

For anyone coming to this question, the error was in firebase SDK. At 1 jul, 2021, firebase team released new firebase sdk v8.7.0 that fixes the issue

see the release notes

Related