I am using Cloud Firestore and i am trying to change my security rule.This is the condition I want
Only users whose phone number is in any documents in the Teacher collection can read the only his/her documents inside the Teacher collection
Here is code i have written
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /Teachers/{teachers}{
allow read: if((resource.data.mobno == request.resource.data.mobno) )
allow update:if((request.resource.data.mobno == resource.data.mobno))
}
}
}
This is the client side query
Teacherref = FirebaseFirestore.getInstance().collection("Teachers");
Teacherref.whereEqualTo("mobno",mobilecredential).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if(!queryDocumentSnapshots.getDocuments().isEmpty()){
for(QueryDocumentSnapshot data:queryDocumentSnapshots){
teacherdata=data.toObject(TeacherLoginModel.class);
if(TextUtils.isEmpty(teacherdata.getStatus()))signinuser(data);
else if(teacherdata.getStatus().equals("signout"))signinuser(data);
else {Toast.makeText(Signin.this, "You Are Already logged In Different Phone", Toast.LENGTH_SHORT).show();
predialog.dismiss();}
}
}
else{
predialog.dismiss();
Toast.makeText(Signin.this,"Please Check Your Credentials ", Toast.LENGTH_SHORT).show(); }
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Signin.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
DocumentReference Teacherref = FirebaseFirestore.getInstance().document("Teachers/"+<current_user_uid>);
instead ofwhereEqualTo
? I am assuming this query is done by the user itself. Then use the if-exists logic? - DharmarajonCreate()
trigger to function your app. This way you won't have to worry about the Firestore rules and also your database paths from your clients. - Dharmaraj