javascript - VueJS + VueFire - 通过身份验证将数据添加到 Firebase 实时数据库

标签 javascript firebase firebase-realtime-database firebase-authentication vuefire

我将 VueJS 与 VueFire 结合使用,并且需要通过用户授权将数据保存到 Firebase 实时数据库

错误消息:

util.js?54b5:189 FIREBASE WARNING: set at /notes/-L5J8Hk8wKHOxH-TQEhb failed: permission_denied exports.warn @ util.js?54b5:189 Repo.js?6ebd:510

Uncaught (in promise) Error: PERMISSION_DENIED: Permission denied

at eval (Repo.js?6ebd:510)
at Object.exports.exceptionGuard (util.js?54b5:556)
at Repo.callOnCompleteCallback (Repo.js?6ebd:501)
at eval (Repo.js?6ebd:278)
at eval (PersistentConnection.js?eae0:411)
at PersistentConnection.onDataMessage_ (PersistentConnection.js?eae0:444)
at Connection.onDataMessage_ (Connection.js?33e2:262)
at Connection.onPrimaryMessageReceived_ (Connection.js?33e2:256)
at WebSocketConnection.eval [as onMessage] (Connection.js?33e2:157)
at WebSocketConnection.appendFrame_ (WebSocketConnection.js?4701:197)

我的 Firebase 授权规则:

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}

VueJS 代码addnote.vue:

<script>
 import Firebase from 'firebase'
  let config = {
    apiKey: '[...]',
    authDomain: '[...].firebaseapp.com',
    databaseURL: 'https://[...].firebaseio.com',
    projectId: '[...]',
    storageBucket: '',
    messagingSenderId: '[...]'
}
  let app = Firebase.initializeApp(config)
  let db = app.database()
  let notesRef = db.ref('notes')

export default {
    name: 'app',
    beforeCreate: function () {
      Firebase.auth().onAuthStateChanged((user) => {
        if (user) {
          this.user = user
          this.$bindAsArray('notes', db.ref(`notes/${user.uid}`))
        }
      })
    },
    firebase: {
      notes: notesRef
    },
    data () {
      return {
        user: null,
        newNote: {
          title: '',
          time: '',
          note: ''
        }
      }
    },
    methods: {
      addNote: function () {
        notesRef.push(this.newNote)
        this.newNote.title = ''
        this.newNote.time = ''
        this.newNote.note = ''
      },
      signInWithGoogle: function () {
        const provider = new Firebase.auth.GoogleAuthProvider()
        Firebase.auth().signInWithRedirect(provider).then((result) => {
          this.user = result.user
        }).catch(error => console.log(error))
      }
    }
}
</script>

最佳答案

您正在收听 /notes 并写入 ``/notes/-L5J8Hk8wKHOxH-TQEhb,而您的规则仅授予您对 /users/$uid 的权限`。由于您没有后者的权限,因此写入失败。

至少您需要更改:

beforeCreate: function () {
  Firebase.auth().onAuthStateChanged((user) => {
    if (user) {
      this.user = user
      this.$bindAsArray('notes', db.ref(`/users/${user.uid}`))
    }
  })
},

还有:

addNote: function () {
    db.ref('users').child(this.user.uid).push(this.newNote)
    this.newNote.title = ''
    this.newNote.time = ''
    this.newNote.note = ''
},

关于javascript - VueJS + VueFire - 通过身份验证将数据添加到 Firebase 实时数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48357862/

相关文章:

javascript - 将字符串数组排序为对象数组

javascript - 通过 id 和 index 索引

Android 依赖 '..' 的编译 (..) 和运行时 (..) 类路径的版本不同

java - 更改内部类中变量的值

android - Firebase 在滚动时动态加载数据

javascript - 从不同子域访问沙盒 iframe 中的摄像头和麦克风

android - 用户在 Firebase 上删除另一个用户

Azure 通知中心填充移动设备上的 "Notifications"区域

android - 获取菜单按下项并将其传递给回收站 View 以设置变量

javascript - 将弹出窗口/信息提示/工具提示显示为悬停特定文本