我正在尝试建立一个规则来获取 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/