如果您使用完全打开/允许所有规则,您似乎只能使Firestore规则工作,从客户端代码调用.add。
这是一个VueJS应用程序。在我的main.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;
在我的Login.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(...)
电话。 我是否错过了这个connect-user-info-to-firestore步骤?这是手动还是自动的东西?
我的客户端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;
//}
}
}
如果我翻转注释以消除第一个allow-all块,并启用只允许request.auth.uid != null
的单个文档,则无法再写入。您在帖子标题中收到权限错误。所以这告诉我正在处理规则,因为注释翻转启用/禁用写入player
集合。
答案 0 :(得分:4)
好的,所以在2010年10月没有太多的firebase / firestore用户:-)我终于找到了答案。上面99.9%的代码没问题。您需要在this.$firebase.auth().onAuthStateChanged(function(user) {...
auth事件处理程序中再添加一行,然后在user.getIdToken().then((accessToken) => {
内:您需要告诉firebase用户accessToken是什么:this.$firebase.auth(accessToken);
。在此之后,我的所有Firestore规则都按预期工作。
确保将firebase ref存储在main.js中的Vue.prototype。$ firebase中。这样您就可以访问所有组件中的firebase。
希望以后可以帮助别人: - )