Firebase 安全读写规则

问题描述 投票:0回答:1

我正在更改 Firebase 安全规则

allow write: if true;

allow write: if request.auth.uid !=null;

但仅当用户通过用户名和密码身份验证登录时才有效,但如果用户通过谷歌登录登录,则它不允许用户写入数据库。

有什么帮助吗??? 谢谢

allow write: ????????    to allow the user signed in via google sign in to access the DB

一旦按下按钮,就会调用此函数

login() {
     googleSignIn.signIn();
  }

在initState中,这段代码是监听登录的

    googleSignIn.onCurrentUserChanged.listen(      
      (account) {
        functionToSignin(account);
      },
      onError: (err) {
        print('Error signing in: $err');
      },
    );

    googleSignIn.signInSilently(suppressErrors: false).then(
      (account) { functionToSignin(account);
      },
    ).catchError((err) { print('Error');   
    });

//-----
functionToSignin(account) async {
    if (account != null) {
      print('got an account from google');
      print('User signed in: $account');
      await addUserInFirestore();// call this function to create and add //users in DB firestore**** 

      setState(() {
        isAuth = true; 
      });
    } else {
      setState(() {
        isAuth = false; 
      });
    }
  }

查询DB的代码是:

final userReference = FirebaseFirestore.instance.collection('users');
final userr = googleSignIn.currentUser;
DocumentSnapshot doc = await userReference.doc(userr?.id).get();

if (!doc.exists) {
userReference .doc(userr?.id).set({
        "id": userr?.id,        
      });

doc = await userReference .doc(userr?.id).get();

非常感谢您的时间和帮助!!

flutter firebase google-cloud-firestore firebase-security
1个回答
0
投票

Firebase 安全规则仅在用户使用 Firebase 身份验证登录时才起作用。看来您直接使用Google登录SDK,这根本不适用于安全规则。如果您希望用户在使用 Google 登录的同时也使用安全规则,则必须按照允许使用 Firebase 身份验证进行 Google 登录的说明进行操作。这将创建一个链接到其 Google 帐户的 Firebase Auth 用户帐户。

© www.soinside.com 2019 - 2024. All rights reserved.