Firebase 实时数据库 - Google Cloud Firestore:FirebaseError: [code


如果您

使用完全开放/允许全部规则,则似乎只能使Firestore规则工作,从客户端代码调用.add。

这是一个 VueJS 应用程序。在我的主要.js...

// You MUST import these 2 lines exactly so
// to get firebase/firestore loaded and working
import firebase from 'firebase';
import 'firebase/firestore';
import config from '../config/firebase.config.json';
firebase.initializeApp(config);
Vue.config.productionTip = false;
// Define some globals: Available to ALL page vues
Vue.prototype.$http = require('axios');
Vue.prototype.$firebase = firebase;

在我的登录.vue中,我有...

methods: {
    loadFirebaseUIAuth() {
        const firebaseUIConfig = {
            'signInSuccessUrl': '/',
            'signInOptions': [
                // Leave the lines as is for the providers you want to offer your users.
                this.$firebase.auth.GoogleAuthProvider.PROVIDER_ID,
                this.$firebase.auth.FacebookAuthProvider.PROVIDER_ID,
                this.$firebase.auth.TwitterAuthProvider.PROVIDER_ID,
                this.$firebase.auth.GithubAuthProvider.PROVIDER_ID
                // firebase.auth.EmailAuthProvider.PROVIDER_ID
            ],
            // Terms of service url.
            'tosUrl': '/tos'
        };
        // Initialize the FirebaseUI Widget using Firebase.
        const firebaseUI = new firebaseui.auth.AuthUI(this.$firebase.auth());
        // The start method will wait until the DOM is loaded.
        firebaseUI.start('#firebaseui-auth-container', firebaseUIConfig);
    },
    initFirebaseAuthHandler() {
        this.$firebase.auth().onAuthStateChanged(function(user) {
            if (user) {
                // User is signed in.
                userData.displayName = user.displayName;
                userData.email = user.email;
                userData.emailVerified = user.emailVerified;
                userData.photoURL = user.photoURL;
                userData.uid = user.uid;
                userData.phoneNumber = user.phoneNumber;
                userData.providerData = user.providerData;
                user.getIdToken().then((accessToken) => {
                    console.log('Login.vue: FirebaseAuthHandler: sign-in-status:', 'Signed in!');
                    userData.accessToken = accessToken;
                    // Store User info, mainly to pass accessToken in request headers
                    localStorage.clear('userData');
                    localStorage.setItem('userData', JSON.stringify(userData));
                });
                console.log('Login.vue: userData: ', userData);
            } else {
                // User is signed out.
                console.log('Login.vue: FirebaseAuthHandler: sign-in-status: ', 'Signed out');
            }
        }, function(error) {
            console.error('Login.vue: FirebaseAuthHandler: ', error);
        });
    }
}

我没有(不是我可以看到)做任何事情来将用户登录信息连接到 Firestore collection.add(...).then(...) 调用。我是否错过了这个将用户信息连接到消防商店的步骤?这是手动还是自动的事情?

我的客户端 Base.data-context.js 创建方法看起来像...

create(collection, model, doneSuccess, doneError) {
    const doneCreate = (doc) => {
        model.attribs = doc;
        return doneSuccess(model);
    };
    delete model.attribs.id; // Do not allow id when creating
    model.attribs.createdby = 'WebUI';
    model.attribs.createdon = new Date();
    model.attribs.modifiedby = 'WebUI';
    model.attribs.modifiedon = new Date();
    model.attribs.modifiedlastip = '';
    collection.add(model.attribs).then(doneCreate).catch(doneError);
}

这是非常通用的。在播放器集合上调用 .add。

在我的 Firestore 规则中,我有...

service cloud.firestore {
    match /databases/{database}/documents {
        match /{document=**} {
            // Any user can read or write this data
            allow read: if true;
            allow write: if true;
        }
        //match /{article=**} {
        //  // Only signed in users can write this data
        //  allow read: if true;
        //  allow write: if request.auth.uid != null;
        //}
        //match /{player=**} {
        //  // Only signed in users can read or write this data
        //  allow read: if request.auth.uid != null;
        //  allow write: if request.auth.uid != null;
        //}
        //match /{character=**} {
        //  // Only signed in users can read or write this data
        //  allow read: if request.auth.uid != null;
        //  allow write: if request.auth.uid != null;
        //}
    }
}

如果我翻转注释以消除第一个允许全部块,并启用应该只允许request.auth.uid != null的单个文档,您将无法再编写。您在帖子标题中收到权限错误。所以这告诉我正在处理规则,因为注释翻转启用/禁用写入player集合。

好的,所以在 2017 年 10 月,SO 上的火碱/火店用户不多:-)我终于找到了答案。上面 99.9% 的代码都没问题。您需要在 this.$firebase.auth().onAuthStateChanged(function(user) {... 身份验证事件处理程序中再增加 1 行,然后在 user.getIdToken().then((accessToken) => { 中:您需要告诉 firebase 用户 accessToken 是什么:this.$firebase.auth(accessToken); 。在此之后,我所有的 Firestore 规则都按预期工作。

确保将 firebase ref 存储在 vue.prototype.$firebase 的 main.js 中。这将使您能够访问所有组件中的火力基地。

希望这对以后有帮助:-)

最新更新