首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Firebase限制对GroupChat中用户的访问

使用Firebase限制对GroupChat中用户的访问
EN

Stack Overflow用户
提问于 2018-07-12 09:11:42
回答 1查看 313关注 0票数 0

我实际上正在构建一个反应本机和火基群聊天,我需要在适当的渠道中将用户分开。我有6个频道与适当的聊天,我不想事实,一个用户可以创建他的频道。在我的应用程序中,我想授权访问A组中的5个用户,B组中的8个用户,等等。

我的规则是:

代码语言:javascript
复制
{
  "rules": {
    "GeneralMessage": {
      ".read": "root.child('user').child(auth.uid).child('inGeneral').child('GeneralMessage').child('read').val() === true",
      ".write": "root.child('user').child(auth.uid).child('inGeneral').child('GeneralMessage').child('write').val() === true"
    }
  }
}

但这不允许我去读和写选项。

编辑:我在房间里推聊天的反应本机代码。

代码语言:javascript
复制
import * as types from './actionTypes'
import firebaseService from '../../services/firebase'

const FIREBASE_REF_MESSAGES = firebaseService.database().ref('/GeneralMessage')
const FIREBASE_REF_MESSAGES_LIMIT = 20

export const sendMessage = message => {
  return (dispatch) => {
    dispatch(chatMessageLoading())

    let currentUser = firebaseService.auth().currentUser
    let createdAt = new Date().getTime()
    let chatMessage = {
      text: message,
      createdAt: createdAt,
      user: {
        id: currentUser.uid,
        email: currentUser.email,
      }
    }

    FIREBASE_REF_MESSAGES.push().set(chatMessage, (error) => {
      if (error) {
        dispatch(chatMessageError(error.message))
      } else {
        dispatch(chatMessageSuccess())
      }
    })
  }
}

export const updateMessage = text => {
  return (dispatch) => {
    dispatch(chatUpdateMessage(text))
  }
}

export const loadMessages = () => {
  return (dispatch) => {
    FIREBASE_REF_MESSAGES.limitToLast(FIREBASE_REF_MESSAGES_LIMIT).on('value', (snapshot) => {
      dispatch(loadMessagesSuccess(snapshot.val()))
    }, (errorObject) => {
      dispatch(loadMessagesError(errorObject.message))
    })
  }
}


const chatMessageLoading = () => ({
  type: types.CHAT_MESSAGE_LOADING
})

const chatMessageSuccess = () => ({
  type: types.CHAT_MESSAGE_SUCCESS
})

const chatMessageError = error => ({
  type: types.CHAT_MESSAGE_ERROR,
  error
})

const chatUpdateMessage = text => ({
  type: types.CHAT_MESSAGE_UPDATE,
  text
})

const loadMessagesSuccess = messages => ({
  type: types.CHAT_LOAD_MESSAGES_SUCCESS,
  messages
})

const loadMessagesError = error => ({
  type: types.CHAT_LOAD_MESSAGES_ERROR,
  error
})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-12 09:49:14

您的数据结构和安全规则并不完全匹配。您有一个关于/user/user.uid/inGeneral/GeneralMessage/read的规则,但是在您的数据结构中inGeneral下没有一个GeneralMessage子级。

使用当前的数据存储,您的规则必须如下所示:

代码语言:javascript
复制
{
  "rules": {
    "GeneralMessage": {
      ".read": "root.child('user').child(auth.uid).child('inGeneral').child('general').val() === true",
      ".write": "root.child('user').child(auth.uid).child('inGeneral').child('general').val() === true"
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51301583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档