2017-10-05 1 views
1

저는 각 애플 리케이션에서 새로운 Firestore를 시험 사용해 보았고 보안 규칙을 고수하고있는 학생 개발자입니다.angular + firestore : 문서에 대한 대중의 읽기 액세스를 허용하는 방법

난 달성하기 위해 노력하고있어 :

디스플레이 바인딩 템플릿을 사용하여 각도보기의 경우 FireStore 문서. 해당 문서는 인증되지 않은 사용자가 볼 수 있어야합니다.

문제 :

인증되지 않은 사용자가 페이지 A 권한 오류를 볼 시도하는 경우가 발생합니다

ERROR Error: Missing or insufficient permissions. at new FirestoreError (error.js:164)

템플릿 파일 :

<p>{{ (item | async)?.name }}</p> 

구성 요소. TS 파일 :

interface Profile { 
    name: string; 
} 
... 
private itemDoc: AngularFirestoreDocument<Profile>; 
item: Observable<Profile>; 
... 
ngOnInit() { 

    this.itemDoc = this.afs.doc<Profile>('profiles/0mlC8uWaKeArzk0sfIfX'); 
    this.item = this.itemDoc.valueChanges(); 
} 

경우 FireStore 규칙 :

service cloud.firestore { 
    match /databases/{database}/documents { 
    match /{document=**} { 
     allow read, write: if request.auth != null; 
    } 
    } 
} 

답변

4

아시다시피, 클라우드 경우 FireStore 데이터에 대한 액세스는 Security Rules에 의해 제어됩니다. "권한 부족 오류"가 표시되면 읽기 또는 쓰기가 규칙에 의해 거부되었음을 의미합니다. 영어로 대략 번역

service cloud.firestore { 
    match /databases/{database}/documents { 
    match /{document=**} { 
     allow read, write: if request.auth != null; 
    } 
    } 
} 

는, 이러한 "읽기 또는 사용자가 로그인되어있는 한 데이터베이스의 모든 문서에 쓰기를 허용"말 : 귀하의 경우

당신은이 규칙이있다.

오류가 발생하는 경우 사용자가 로그인하지 않았 음을 의미합니다 (request.auth == null). 당신은 앱에 Firebase Authentication을 추가 할 수 있습니다 귀하의 앱

에 인증을 추가

옵션 1 :

그래서 두 가지 옵션이 있습니다.

firebase.auth().signInAnonymously() 
.then(function() { 
    // You're signed in, reads will work 
}) 
.catch(function(error) { 
    // Handle Errors here. 
    // ... 
}); 

옵션 2 : 규칙을 만족시킬 것이다 가장 간단한 것은 익명 인증이 될 것입니다 귀하의 보안 규칙 변경

모든 사용자가 앱의 모든 데이터를 읽을 수 있도록하려면 다음과 같이 규칙을 열 수 있습니다.

service cloud.firestore { 
    match /databases/{database}/documents { 
    match /{document=**} { 
     allow read: if true; 
     allow write: if request.auth != null; 
     } 
    } 
} 
+0

감사합니다. 나는 규칙을 올바르게 읽지 않았다. –

0
service cloud.firestore { 
    match /databases/{database}/documents { 
    match /{document=**} { 
     allow read; 
     allow write: if request.auth != null; 
     } 
    } 
} 
관련 문제