简体   繁体   English

如果 firebase 规则设置为 auth !=null,如何检索 AngularList

[英]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.

相关问题 firebase中如何给用户设置规则? - How to set rules to users in firebase? 如何在Firebase auth中设置用户名 - How to set username in Firebase auth Firebase 安全规则:“auth != null”不适用于来自 firebase-admin 的查询 - Firebase Security Rules: "auth != null" does not work for queries from firebase-admin 如何通过 Firebase 设置自定义身份验证声明并识别平台 - How to set custom auth claims through Firebase and identify platform 可以使用 Firebase Auth.auth() 函数检索 GoogleSignIn 用户吗? - Can Firebase Auth.auth() functions be used to retrieve a GoogleSignIn user? Firebase Auth ID 令牌验证的必要性,即使有数据库的安全规则? - Necessity of Firebase Auth ID token validation, even if there are security rules of database? Firebase 安全规则:检查数据库值是否与检查身份验证同样安全? - Firebase Security Rules: Is checking database value equally secure with checking auth? Firebase 身份验证失败,安全性为 null - Firebase Auth failing with null safety 如何使用 firebase 规则进行身份验证(RESTful) - How to authenticate with firebase rules (RESTful) 即使规则在 Firebase 实时数据库中设置为 false,我如何以管理员身份读写 - how can I read and write as an admin even if the rules set to false in Firebase Realtime Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM