Adrita Rahman
Adrita Rahman

Reputation: 189

Unhandled Exception: PlatformException(Error performing get, PERMISSION_DENIED: Missing or insufficient permissions., null)

Whenever the app starts this error comes. i think the error mainly is in the saveUserInfoFirestore function.. i saw that many other people have this same problem cause of their firestore security rules but i tried differenct rules it still shows same error..

final GoogleSignIn gSignIn = GoogleSignIn();
final userReference = Firestore.instance.collection("Users");    
void initState(){
        super.initState();
        pageController=PageController();
        gSignIn.onCurrentUserChanged.listen((gSigninAccount) { ControlSingIn(gSigninAccount); },
            onError: (gError){
                print("Google signin error"+ gError);
            });
        gSignIn.signInSilently(suppressErrors: false).then((gSigninAccount) { ControlSingIn(gSigninAccount); }).
        catchError((onError){
          print("Signin silently error"+ onError);
        });
      }
    
      ControlSingIn(GoogleSignInAccount signInAccount) async{
    
        if(signInAccount != null){
          await saveUserInfotoFirestore();
          setState(() {
            isSignin = true;
          });
        }else{
          setState(() {
            isSignin = false;
          });
        }
    
      }
      saveUserInfotoFirestore() async{
          final GoogleSignInAccount gCurrentuser = gSignIn.currentUser;
          DocumentSnapshot documentSnapshot = await userReference.document(gCurrentuser.id).get();
    
          if(!documentSnapshot.exists){
            final username = await Navigator.push(context, MaterialPageRoute(builder: (context)=>CreateAccountPage()));
    
            userReference.document(gCurrentuser.id).setData({
              "id": gCurrentuser.id,
              "profileName": gCurrentuser.displayName,
              "username": username,
              "url": gCurrentuser.photoUrl,
              "email": gCurrentuser.email,
              "bio": "",
              "timestamp":timestamp
            });
            documentSnapshot = await userReference.document(gCurrentuser.id).get();
    
          }
          currentUser = User.fromDocument(documentSnapshot);
    
      }

heres the firestore security rule that i am using:

rules_version = '2';
// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
  match /databases/{database}/documents {
    match /messages/{document=**} {
      allow read, write: if true;
    }
  }
}

Upvotes: 0

Views: 734

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317828

Your security rules only allow access to a single collection called "messages" using this match:

    match /messages/{document=**} {
      allow read, write: if true;
    }

However, your code is trying to write a different collection called "Users". You are seeing a permission error because your rules don't allow any access at all to that collection. You will have to write another rule to allow enough access to that collection as well.

I strongly suggest fully reviewing the documentation for security rules to understand how best to protect your app.

Upvotes: 1

Sherlie
Sherlie

Reputation: 81

check with your googleservices.json file if it is in the right location. It should be in your project>android folder> app folder>

Upvotes: 0

Related Questions