Warm tip: This article is reproduced from serverfault.com, please click

firebase-onSnapshot中未捕获的错误:错误:signOut()上缺少权限或权限不足

(firebase - Uncaught Error in onSnapshot: Error: Missing or insufficient permissions on signOut())

发布于 2018-12-02 21:42:27

我使用vuex和火力点实施的指令后用户认证vuegram我尝试了多种方法来分离Firebase侦听器,以下是唯一一种停止警告错误的方法:

var unsubscribe=fb.auth.onAuthStateChanged(user=>{
    if(user){
        store.commit('setCurrentUser',user)
        store.dispatch('fetchUserProfile')

        fb.usersCollection.doc(user.uid).onSnapshot(doc => {
            store.commit('setUserProfile', doc.data())
        })
    }
})
unsubscribe();

但是,上面的代码只是在signOut()上停止警告,我再也不能更新数据了。

我的store.js文件:

var unsubscribe=fb.auth.onAuthStateChanged(user=>{
    if(user){
        store.commit('setCurrentUser',user)
        store.dispatch('fetchUserProfile')

        fb.usersCollection.doc(user.uid).onSnapshot(doc => {
            store.commit('setUserProfile', doc.data())
        })
    }
})

export const store=new Vuex.Store({
    state:{
        currentUser:null,
        userProfile:{}
    },
    actions:{
        clearData({commit}){
            commit('setCurrentUser',null)
            commit('setUserProfile', {})
        },
        fetchUserProfile({ commit, state }) {
             fb.usersCollection.doc(state.currentUser.uid).get().then(res => {
                commit('setUserProfile', res.data())

            }).catch(err => {
                console.log(err)
            })
        },
        updateProfile({ commit, state }, data) {
            let displayName = data.displayName

            fb.usersCollection.doc(state.currentUser.uid).set({
                displayName: displayName
            }, {merge:true}).then(function() {
                alert("Document successfully written!");
            })
            .catch(function(error) {
                alert("Error writing document: ", error);
            });
        }
    },
    mutations:{
        setCurrentUser(state, val) {
            state.currentUser = val
        },
        setUserProfile(state, val) {
            state.userProfile = val
        }
    }
})

signOut方法:

signOut: function(){
        fb.auth.signOut().then(()=> {
            this.$store.dispatch('clearData')
            this.$router.push('login')
        }).catch(function(error) {
            console.log(error);
        });
    }

我的Firebase规则:

allow read, write: if request.auth.uid!=null;
Questioner
JackJack
Viewed
22
Frank van Puffelen 2018-12-03 06:52:30

由于注销时你仍然具有活动的侦听器,因此系统检测到客户端失去了读取该数据的权限,因此拒绝了该侦听器。这意味着你需要在注销之前删除侦听器,以防止出现错误消息。

请参阅有关分离侦听器文档,你在附加侦听器时首先会获得对unsubscribe函数的引用:

unsubscribe = fb.usersCollection.doc(user.uid).onSnapshot(doc => {
    store.commit('setUserProfile', doc.data())
})

然后在退出之前调用该函数:

signOut: function(){
    unsubscribe();
    fb.auth.signOut().then(()=> {
        this.$store.dispatch('clearData')
        this.$router.push('login')
    }).catch(function(error) {
        console.log(error);
    });
}