json - firestore授权规则: for google signed in client

标签 json firebase firebase-authentication firebase-security google-cloud-firestore

我正在尝试建立一个规则来获取 firestore 数据,该数据可以由 Google 登录的客户端访问。 所以我面临的问题是当我使用这个规则时

match /helpers/customer/data/{document=**}{
  allow read: if request.auth != null;
}

logcat 中弹出错误

onFailure: Errorcom.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.

而且它仅在我使用时才有效

match /helpers/customer/data/{document=**}{
  allow read: if true;
}

这意味着路径已写入。

GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(this);

    if(acct != null){
        Log.i(TAG, "onCreate: Database Working");
        mFirestoreDB
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (DocumentSnapshot document : task.getResult()) {
                                Log.d(TAG, document.getId() + " => " + document.getData());
                            }
                        } else {
                            Log.d(TAG, "Error getting documents: ", task.getException());
                        }
                    }
                });
    }else{
        Log.i(TAG, "onCreate: Database not Working");
    }

我需要的是一条规则,仅允许 Google 登录的用户访问。

最佳答案

使用 Google 登录不会自动使用户登录 Firebase。您还需要使用 Firebase 身份验证登录它们,然后您的安全规则才会设置其 auth 变量。

来自Firebase documentation in signing in with Google :

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "signInWithCredential:success");
                        FirebaseUser user = mAuth.getCurrentUser();
                        updateUI(user);
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        Toast.makeText(GoogleSignInActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                        updateUI(null);
                    }

                    // ...
                }
            });
}

但我建议您阅读我链接的整个页面。

关于json - firestore授权规则: for google signed in client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47669081/

相关文章:

java - 尝试实现自己的反序列化器时出现 Gson + AutoValue 错误

json - 将响应数据处理到Struc中

angular - 在 ionic 3 中使用 firebase 使用电话号码进行身份验证

android - Firebase 管理多个身份验证提供程序

ios - 给定用户 UID,我可以访问他的个人资料信息吗?

javascript - 如何检查 Firebase Web 中的身份验证过程是否没有错误?

python - 将 Twitter 时间转换为特定格式的日期时间,以统计一天中推文的频率

json - Python Json 引用和验证

firebase - Flutter firestore 复合查询

Firebase throw "the server responded with status 400"