权限被拒绝:Android上缺少权限或权限不足,但正在为ios Firestore工作
我试图在android设备中更新firestore中的数据库,但收到错误“权限缺失或不足”。如果这是由于规则的原因,那么同样的规则在iOS中也适用,但在android中则不行。有人能给我指出一条正确的规则吗。下面是我使用的规则权限被拒绝:Android上缺少权限或权限不足,但正在为ios Firestore工作,android,ios,google-cloud-firestore,firebase-security,Android,Ios,Google Cloud Firestore,Firebase Security,我试图在android设备中更新firestore中的数据库,但收到错误“权限缺失或不足”。如果这是由于规则的原因,那么同样的规则在iOS中也适用,但在android中则不行。有人能给我指出一条正确的规则吗。下面是我使用的规则 rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { // This rule allows anyone on the internet
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// This rule allows anyone on the internet to view, edit, and delete
// all data in your Firestore database. It is useful for getting
// started, but it is configured to expire after 30 days because it
// leaves your app open to attackers. At that time, all client
// requests to your Firestore database will be denied.
//
// Make sure to write security rules for your app before that time, or else
// your app will lose access to your Firestore database
match /users/{userId} {
allow read, update, delete: if request.auth != null;
allow create: if request.auth != null;
}
}
}
我用来更新记录的代码
db.collection("user").document(documentSnaapShotId).update(userData).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
sendUserToInitialPage();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
String error = e.getMessage();
Toast.makeText(UpdateUserActivity.this, "Error:" + error, Toast.LENGTH_LONG).show();
}
});
请编辑您的问题以显示触发错误消息的代码,以及确切的错误消息和堆栈跟踪。HI@FrankvanPuffelen我已经更新了上面我在android studio中用于更新记录的代码。“…以及确切的错误消息和堆栈跟踪”还要注意,您共享的代码中没有任何内容显示
if request.auth!=无效代码>已满足。一个非常简单的方法是在添加数据之前立即System.out.println(FirebaseAuth.getInstance().getCurrentUser().getUid()
。更新了inline@FrankvanPuffelen的进一步调查
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
mFirestore = FirebaseFirestore.getInstance();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
mFirestore.collection("mydbexample").whereEqualTo("uid", uid).addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if(e != null) {
Toast.makeText(PhoneAuthActivity.this, "Error:" + e, Toast.LENGTH_LONG).show();
}
for(DocumentSnapshot doc : queryDocumentSnapshots) {
docId = doc.getId();
userFname = doc.getString("firstname");
userLname = doc.getString("lastname");
userCountry = doc.getString("country");
userState = doc.getString("state");
userCity = doc.getString("city");
userArea = doc.getString("area");
documentExists = (userFname!= null);
}
if(documentExists){
sendUserToInitialPage();
}
else {
sendUserToHome();
}
}
});
} else {
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
Toast.makeText(getApplicationContext(),
"Incorrect Verification Code ", Toast.LENGTH_LONG).show();
}
}
}
});
}
private void sendUserToHome() {
Intent homeIntent = new Intent(PhoneAuthActivity.this, GuestUserActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);
finish();
}
private void sendUserToInitialPage() {
Intent homeIntent = new Intent(PhoneAuthActivity.this, InitialPageActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(homeIntent);
finish();
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.virtualbloodbank, PID: 489
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator com.google.firebase.firestore.QuerySnapshot.iterator()' on a null object reference
at com.app.virtualbloodbank.PhoneAuthActivity$4$1.onEvent(PhoneAuthActivity.java:224)
at com.app.virtualbloodbank.PhoneAuthActivity$4$1.onEvent(PhoneAuthActivity.java:217)
at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2(com.google.firebase:firebase-firestore@@21.4.0:1039)
at com.google.firebase.firestore.Query$$Lambda$3.onEvent(Unknown Source:6)
at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0(com.google.firebase:firebase-firestore@@21.4.0:42)
at com.google.firebase.firestore.core.AsyncEventListener$$Lambda$1.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)