[英]How to retrieve AngularList if firebase rules is set to auth !=null
My issue is that I do not know can I retrieve data from firabase using AngularFireList.我的问题是我不知道能否使用 AngularFireList 从 firabase 检索数据。 If I set database rules as with data can be used only by authorized user:
如果我将数据库规则设置为数据只能由授权用户使用:
{
"rules": {
".read": "auth !=null", // 2021-1-28
".write": "auth !=null", // 2021-1-28
}
}
Of course my previous fetch logic dose not work, but I do not know how to tell firebase, that user is authorized.当然,我以前的获取逻辑不起作用,但我不知道如何告诉 firebase,该用户已获得授权。 This is my service function:
这是我的服务 function:
constructor(
private db: AngularFireDatabase,
private authService: AuthService,
public afAuth: AngularFireAuth) {
}
getItems(){
this.pacRef = this.db.list('pacients');
this.pacientsData = this.pacRef.snapshotChanges().pipe(
map(changes=> changes.map(a=>({
key : a.payload.key, ...a.payload.val()
}))));
return this.pacientsData;
}
And here I component where I am trying get data from firabase using gatItems(key) function.在这里,我尝试使用 gatItems(key) function 从 firabase 获取数据。
constructor(private router:Router,
private pacientService: PacientServiceService,
private authService: AuthService) {
}
ngOnInit(): void {
this.userSub = this.authService.user.subscribe(user=>{
this.isAuthenticated = !!user;
this.userToken = user.token;
console.log(this.isAuthenticated);
if (this.isAuthenticated) {
this.pacientService.getItems().subscribe(pacientsData=>{
this.pacientsData = pacientsData;
});
}
});
}
Picture from console: enter image description here Picture from authorization enter image description here图片来自控制台:在此处输入图片描述图片来自授权在此处输入图片描述
Authorization works and I am getting back token.授权有效,我正在取回令牌。 Any help can help:) This only second month when I building something with Angular.
任何帮助都可以提供帮助:) 这只是我用 Angular 构建东西的第二个月。
Given your security rules, you should only call getItems
once the user is authenticated.鉴于您的安全规则,您应该只在用户通过身份验证后调用
getItems
。
So:所以:
ngOnInit(): void {
this.userSub = this.authService.user.subscribe(user=>{
this.isAuthenticated = !!user;
this.userToken = user.token;
console.log(this.isAuthenticated);
if (this.isAuthenticated) {
this.pacientService.getItems(this.userToken).subscribe(pacientsData=>{
this.pacientsData = pacientsData;
});
}
});
}
I also think you don't need to pass the auth key yourself, as the Firebase SDKs handle that behind the scenes.我还认为您不需要自己传递身份验证密钥,因为 Firebase SDK 会在幕后处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.