正在获取未处理的异常:[cloud_firestore/permission-denied].我已经检查了firebase中的一切都正常

kse8i1jr  于 2022-11-25  发布在  其他
关注(0)|答案(1)|浏览(142)

大家好,我已经创建了flutter应用程序,其中当用户按下注册按钮,然后他们将检查提供的电子邮件是否存在于firestore数据库中,如果没有,那么他们将被移动到下一个屏幕。我得到这个[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.异常,即使一切看起来都很好,在我的代码和firestore配置
下面是我的firestore数据库规则:

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

下面是我的项目级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
}

这是我的应用程序级版本。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')
}

以下是我的注册代码:

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*/
    }
  }
}

用户模型类checkIfEmailExist函数

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;
  }
}
owfi6suc

owfi6suc1#

解决方案:在Firestore数据库规则中:

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

相关问题