无法将基本身份验证安全规则设置为FIRESTORE

时间:2018-03-13 23:51:49

标签: react-native firebase-authentication google-cloud-firestore expo firebase-security-rules

我试图将规则设置为Firestore,允许所有用户在他们登录时进行读写。

我已使用电子邮件和密码验证我的身份验证使用Firebase。

但是我写的最简单的auth规则,它只是给我一个错误。

我试图让Firestore使用此规则:

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

但是当我尝试写信给Firestore时,我得到了这个:

Error throw by Firestore when adding basic Auth Rules

如果我尝试功能助手方法:

 service cloud.firestore {
   //Function
   function isSignedIn() {
     return request.auth.uid != null;
 //Also tried with "request.auth != null"
   }
   //Rules
   match /databases/{database}/documents {
     match /{document=**} {
       allow read: if isSignedIn();
       allow write: if isSignedIn();
 //Also tried with "allow read, write: if isSignedIn();"
     }
   }
 }

它也给我带来了同样的错误。

我也尝试过匹配自定义路径,例如match /Restaurants/{RestaurantsId} ...,但也没有用...

但如果删除所有内容,我会使用以下默认规则:

 service cloud.firestore {
   match /databases/{database}/documents {
     match /{document=**} {
       allow read, write;
     }
   }
 }

它确实有用......

我已经关注了Firestore文档,AngularFirebase视频教程,查看了github以及类似的情况,我仍然无法使其工作。我看到所有试过这个的人都能完美地工作,但它不适合我:S。

----更新1 ----

以下是我如何调用Firestore的set方法:

当我按下“保存按钮”时,我称之为帮助功能:

saveMealHandler() {
    const { mealImage, mealCategory, mealName, mealIngredients, mealPrice, mealOptions, mealExtras, user } = this.props;
    const id = this.props.completeMenu.length;
    const { navigation } = this.props;
    this.props.saveMeal({
      key: id,
      name: mealName,
      avatar: mealImage,
      ingredients: mealIngredients,
      price: mealPrice,
      category: mealCategory,
      options: mealOptions,
      extras: mealExtras
    }, user);
    this.props.cleanAllInputs();
  }

并且此助手调用是在Firestore上写入的以下操作创建者:

export const saveMeal = (newMeal, user) => {
  const db = firebase.firestore();
  const docRef = db.collection('Resto').doc(user);
  return dispatch => {
    docRef.set({
        isWorking: 'JALAPEÑO!'
    })
    .then(() => {
        console.log('Document successfully written!');
    })
    .catch((error) => {
        console.error('Error writing document: ', error);
    });
    dispatch({ type: SAVE_NEW_MEAL_TO_MENU, payload: newMeal });
  };
};

----更新1 ----

----更新2 ----

这是捕获用户和密码并调用动作创建者的函数:

  onButtonPress() {
    const { email, password } = this.props;
    console.log(email, password);
    this.props.loginUser({ email, password });
  }

这是我用于注册和登录用户的动作创建者

export const loginUser = ({ email, password }) => {
  return (dispatch) => {
    dispatch({ type: LOGIN_USER_START });
    firebase.auth().signInWithEmailAndPassword(email, password)
      .then(user => userLoginSuccess(dispatch, user))
      .catch(() => {
        firebase.auth().createUserWithEmailAndPassword(email, password)
          .then(user => userLoginSuccess(dispatch, user))
          .catch(() => userLoginFail(dispatch));
      });
  };
};
//Helpers
const userLoginSuccess = (dispatch, user) => {
  dispatch({ type: LOGIN_USER_SUCCESS, payload: user.uid });
};
const userLoginFail = (dispatch) => {
  dispatch({ type: LOGIN_USER_FAIL });
};

这就是我知道这些功能正在发挥作用的方式:

enter image description here

----更新2 ----

我想我的所有尝试都可能是我错过了在不知情的情况下安装某些东西或者在添加条件时出现问题。但是这一刻我只想问。

我还录制了一个视频,以便您可以看到问题,这里是:

https://youtu.be/9fmWk7-HjZ4

感谢大家。