Home > Blockchain >  Getting Unhandled Exception: [cloud_firestore/permission-denied]. I have checked everything was fine
Getting Unhandled Exception: [cloud_firestore/permission-denied]. I have checked everything was fine

Time:11-21

Hello Guys I had created the flutter application in which when a user pressed signup button then they will be checked that provided email exists in the firestore database or not if no then they will be move to next screen. I am getting this [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [cloud_firestore/permission-denied] The caller does not have permission to execute the specified operation. exception even everything looks fine in my code and firestore configuration

Here is my firestore database rules:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

Here is my project level build.gradle:

buildscript {
    ext.kotlin_version = '1.6.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.13'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Here is my app-level build.gradle

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs  = 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.recipedia.fyp.recipedia"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
        minSdkVersion 21
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:31.0.3')
}

Here is my signup code:

onPressed: () async {
  if (nameTextController.text.isEmpty) {
    displayToastMessage(
      "Please enter name", context);
  } else if (nameTextController.text.length < 3) {
    displayToastMessage("Name must be atleast 3 characters", context);
  } else if (nameTextController.text.contains(RegExp(r'[0-9]'))) {
    displayToastMessage("Numbers and special characters cannot be included", context);
  } else if (emailTextController.text.isEmpty) {
    displayToastMessage("Please enter email", context);
  } else if (!emailTextController.text.contains('@')) {
    displayToastMessage("Please enter a valid email", context);
  } else if (passwordTextController.text.isEmpty) {
    displayToastMessage("Please enter password", context);
  } else if (passwordTextController.text.length < 6) {
    displayToastMessage("Password must be at-least 6 Characters", context);
  } else {
    print('Before emailExists');
    emailExists = await UserModel().checkIfEmailExists(email);
    print('Email exist: $emailExists');
    if (emailExists == true) {
    snackBar(context, 'Email is already registered');
    } else {
      /*Move to next screen*/
    }
  }
}

User Model class checkIfEmailExist function

Future<bool> checkIfEmailExists(String email) async {
  try {
    var collectionReference = FirebaseFirestore.instance.collection('users');
    var doc = await collectionReference.doc(email).get();
    return doc.exists;
  } catch (e) {
    rethrow;
  }
}

CodePudding user response:

Solution: In Firestore database rule:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}
  • Related