Auth with multiple firebase realtime databases on web not working

◇◆丶佛笑我妖孽 提交于 2021-01-28 05:26:17

问题


I have two Firebase realtime databases. I have managed to connect, read, and write to both of them from my web-based client. However, I am going back through my security rules and made it so you can only read/write if you are authenticated using

  "rules": {
    ".read": "auth.uid != null" ,
    ".write": "auth.uid != null" 
  }

However, for some reason I am now unable to connect to the database, it says that permission is denied. I am authenticated on the client using Google, and everything else with auth works as expected. For example, I'm able to print out the user's uid and everything. Here's the client side code I have (censored).

  var firebaseConfig = {
apiKey: "------------------",
authDomain: "-------------",
databaseURL: "-------------",
projectId: "-------",
};
// Initialize Firebase
var app = firebase.initializeApp(firebaseConfig, 'app2');

firebase.database(app).ref('boards').child(boardID).once('value', function(snapshot) {//I do stuff here})

Error:

Error: permission_denied at /boards/id: Client doesn't have permission to access the desired data.
    at util.ts:526
    at onComplete (SyncTree.ts:651)
    at Object.onComplete (Repo.ts:167)
    at PersistentConnection.ts:247
    at si.onDataMessage_ (PersistentConnection.ts:554)
    at ei.onDataMessage_ (Connection.ts:317)
    at ei.onPrimaryMessageReceived_ (Connection.ts:309)
    at $r.onMessage (Connection.ts:205)
    at $r.appendFrame_ (WebSocketConnection.ts:273)
    at $r.handleIncomingFrame (WebSocketConnection.ts:326)

And these are the firebase scripts I'm using

<script src="/__/firebase/7.6.1/firebase-app.js"></script>
<script src="/__/firebase/7.6.1/firebase-database.js"></script>
<script src="/__/firebase/7.8.1/firebase-auth.js"></script>
<script src="/__/firebase/init.js"></script>

Is this a known bug? Am I missing something? Thanks.


回答1:


I solved this issue by switching from

<script src="/__/firebase/7.6.1/firebase-app.js"></script>
<script src="/__/firebase/7.6.1/firebase-database.js"></script>
<script src="/__/firebase/7.8.1/firebase-auth.js"></script>
<script src="/__/firebase/init.js"></script>

to

  <script src="https://code.jquery.com/jquery-latest.min.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.9.0/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.9.0/firebase-database.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.9.0/firebase-auth.js"></script>

And removing the , 'app2) from var app = firebase.initializeApp(firebaseConfig, 'app2');

This made the second database be the default database and now everything is working properly. However, if there is ever a time where both database references are required on the client, this will once again become a problem, and as far as I can tell cannot be fixed unless the Firebase developers release a patch.




回答2:


Firebase Authentication restores the user's authentication state when you reload the page. This requires a call to the server, which happens asynchronously. Most likely the authentication check hasn't finished yet when you attach the listener.

To fix this, always attach listeners that require authentication in an onAuthStateChanged callback:

// Initialize Firebase
var app = firebase.initializeApp(firebaseConfig, 'app2');

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    firebase.database(app).ref('boards').child(boardID).once('value', function(snapshot) {//I do stuff here})
  }
}

If you ever have similar problems again, I recommend logging the active user's UID to help in troubleshooting:

console.log(firebase.auth().currentUser ? firebase.auth().currentUser.uid : "No user");
firebase.database(app).ref('boards').child(boardID).once('value', function(snapshot) {//I do stuff here})

This will often make it immediately obvious why the security rules are rejecting your write.



来源:https://stackoverflow.com/questions/60340766/auth-with-multiple-firebase-realtime-databases-on-web-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!