如何解决使用模拟器时,Firestore 规则中的 request.auth 为 null
我正在通过 AngularFire
和 Firebase Emulators
测试 Firestore 规则。
我的firestore rule
很简单,只检查用户是否登录。
问题是不允许任何用户访问 Firestore
,尽管用户经过身份验证。
但是当我不使用 Emulator(即使用真正的 Firestore )时,它是允许的。
所以我想知道我在使用Emulator时是否有额外的设置? 请帮帮我!
浏览器控制台:
core.js:6210 ERROR Error: Uncaught (in promise): FirebaseError: [code=permission-denied]: PERMISSION_DENIED:
false for 'create' @ L5
我的环境:
- firebase 工具:9.8.0
- 角度:11.2.12
- @angular/fire: 6.1.4
我的 Firestore 规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read,write: if request.auth != null;
}
}
}
app.module.ts:
@NgModule({
declarations: [
AppComponent
],imports: [
browserModule,AngularFireModule.initializeApp(environment.firebaseConfig),AngularFireAuthModule,AngularFirestoreModule,],providers: [
{ provide: USE_AUTH_EMULATOR,useValue: environment.useEmulators ? ['localhost',9099] : undefined },{ provide: USE_DATABASE_EMULATOR,9000] : undefined },{ provide: USE_FIRESTORE_EMULATOR,8080] : undefined },bootstrap: [AppComponent]
})
export class AppModule { }
app.component.html:
<button (click)="onLoginButtonClick()">change status(current: {{status}})</button><br>
app.component.ts:
export class AppComponent {
status = 'not logged in';
constructor(
private afAuth: AngularFireAuth,private afStore: AngularFirestore
) {
this.afAuth.authState
.subscribe((user) => {
if ( user != null ) {
// If user logged in,then add new data.
this.afStore
.collection('test_data')
.doc(user.uid)
.set({data: 'test data'}); // this causes error!
}
});
}
async onLoginButtonClick(): Promise<void>{
if ( this.status === 'not logged in'){
const provider = new firebase.auth.GoogleAuthProvider();
await this.afAuth.signInWithPopup(provider);
this.status = 'logged in';
}
else{
await this.afAuth.signOut();
this.status = 'not logged in';
}
}
}
解决方法
毕竟,我尝试升级firebase-tools
,它的版本变成了9.8到9.10,然后我的问题就解决了。
我发现了这个 issue,这似乎是 Firebase Emulator 的问题。
谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。