繁体   English   中英

通过保留的 URL 加载 Firebase SDK 出现“权限被拒绝”错误

[英]Loading Firebase SDKs via reserved URL is giving "Permission denied" error

我已设法使用文档中所述的保留 url 部署我的 Firebase 站点。 当我的数据库规则设置为“true”时一切正常,但是一旦我尝试放入规则,它就会抛出错误“权限被拒绝”

我没有更改 index.html 的默认代码,只是添加了简单的行来尝试包含我尝试读取或写入的节点的路径。 我已经设法在 NodeJS 和 FirebaseUI 中使用这些规则通过另一个站点登录,但我似乎无法按照文档中的建议自动加载我的 sdk 配置。 任何人都可以提供一些见解吗? 谢谢。

编辑:我刚刚成功尝试使用 firebase.auth() 创建一个新用户。 显然,我的配置实际上已被自动导入。 我不知道为什么我无法读取或写入数据库,除非作为规则范围内的用户。 我认为 Admin SDK 会覆盖所有数据库规则。 我错过了一些明显的东西吗?

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Welcome to Firebase Hosting</title> <!-- update the version number as needed --> <script defer src="/__/firebase/7.21.1/firebase-app.js"></script> <!-- include only the Firebase features as you need --> <script defer src="/__/firebase/7.21.1/firebase-auth.js"></script> <script defer src="/__/firebase/7.21.1/firebase-database.js"></script> <script defer src="/__/firebase/7.21.1/firebase-storage.js"></script> <!--script src="https://cdn.firebase.com/libs/firebaseui/3.5.2/firebaseui.js"></script--> <!-- initialize the SDK after all desired features are loaded --> <script defer src="/__/firebase/init.js"></script> <style media="screen"> body { background: #ECEFF1; color: rgba(0, 0, 0, 0.87); font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 0; } #message { background: white; max-width: 360px; margin: 100px auto 16px; padding: 32px 24px; border-radius: 3px; } #message h2 { color: #ffa100; font-weight: bold; font-size: 16px; margin: 0 0 8px; } #message h1 { font-size: 22px; font-weight: 300; color: rgba(0, 0, 0, 0.6); margin: 0 0 16px; } #message p { line-height: 140%; margin: 16px 0 24px; font-size: 14px; } #message a { display: block; text-align: center; background: #039be5; text-transform: uppercase; text-decoration: none; color: white; padding: 16px; border-radius: 4px; } #message, #message a { box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24); } #load { color: rgba(0, 0, 0, 0.4); text-align: center; font-size: 13px; } @media (max-width: 600px) { body, #message { margin-top: 0; background: white; box-shadow: none; } body { border-top: 16px solid #ffa100; } } </style> </head> <body> <div id="message"> <h2>Welcome</h2> <h1>Firebase Hosting Setup Complete</h1> <p>You're seeing this because you've successfully setup Firebase Hosting. Now it's time to go build something extraordinary!</p> <a target="_blank" href="https://firebase.google.com/docs/hosting/">Open Hosting Documentation</a> </div> <p id="load">Firebase SDK Loading&hellip;</p> <script> document.addEventListener('DOMContentLoaded', function () { firebase.database().ref('/test').set('just a test') // // 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥 // // The Firebase SDK is initialized and available here! // // firebase.auth().onAuthStateChanged(user => { }); firebase.database().ref('/users').on('value', snapshot => { console.log(snapshot.val()) console.log(snapshot.key)}); // firebase.messaging().requestPermission().then(() => { }); // firebase.storage().ref('/path/to/ref').getDownloadURL().then(() => { }); // // // 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥 try { let app = firebase.app(); let features = ['auth', 'database', 'messaging', 'storage'].filter(feature => typeof app[feature] === 'function'); document.getElementById('load').innerHTML = `Firebase SDK loaded with ${features.join(', ')}`; } catch (e) { console.error(e); document.getElementById('load').innerHTML = 'Error loading the Firebase SDK, check the console.'; } }); </script> </body> </html>

 /* { "rules": { ".read": true, ".write": true } } */ { "rules": { "users": { "$user_id": { ".write": "$user_id === auth.uid", ".read": "$user_id === auth.uid" }, "Google_logins": { ".read":true, ".write":true } } } }

您的网页未使用 Admin SDK,而是使用常规 JavaScript SDK。 这意味着您的代码必须遵守您自己的安全规则和这一行:

firebase.database().ref('/users').on('value', snapshot => { 

不起作用,因为您的安全规则没有授予任何人读取/users节点的权限。

如果您希望用户从数据库中读取他们自己的节点,那就是:

firebase.auth().onAuthStateChanged(user => { 
  if (user) {
    firebase.database().ref('/users').child(user.uid).on('value', snapshot => { 
      ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM