1
votes

I am developing firebase webapp and faced one problem. I cannot access(read/write) firebase realtime database.

 <script src="https://www.gstatic.com/firebasejs/4.12.0/firebase.js"></script>
<script>
var config = {
    apiKey: "AIzaSyCPYu_iqHjNoXjMfdMVLoYs5Graf2gdz7I",
    authDomain: "sharing-is-caring-967c8.firebaseapp.com",
    databaseURL: "https://sharing-is-caring-967c8.firebaseio.com",
    projectId: "sharing-is-caring-967c8",
    storageBucket: "sharing-is-caring-967c8.appspot.com",
    messagingSenderId: "884406512169"
  };
firebase.initializeApp(config);

    firebase.auth().signInWithEmailAndPassword("[email protected]", "Qqqq123").then(function(info) {
        var uid = info.uid;
        firebase.database().ref('users/' + uid + '/userDetails').once('value').then(function(data) {
            alert(data)
        }).catch(function(error) {
            alert(error.message);
        });
    }).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        alert(errorMessage);
        // ...
    });
</script>

SignInWithEmailAndPassword work fine. And At database part, once('value') function does not response anymore. There is no success and no fail. There isn't any error message and always silence

Rules of Realtime Database

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
      "threads": {
        "$tid": {
          "messages": {
            ".indexOn": ["read", "date"]
          }
        }
      }
  }
}

Fire base Web Setup Guide Screenshot of my Firebase

Database structure

{
  "users" : {
    "BzucCBivhiRUogK5GeNrMV6k6M83" : {
      "userDetails" : {
        "displayName" : "Stanislav Sergeev",
        "email" : "[email protected]",
        "notifications" : true,
        "phoneNumber" : "",
        "profile_image" : "",
        "pushToken" : "ec7iJ7Mt7Lg:APA91bFb8NK2XeaLVUgr-DEPRFuLMDCk9_tIr_eHBjmuZPnHDUvFuCsj3kez4F5-Y9CjVXOY6VlsFi5nlazSZys4KBjwCKcpINbV5S2ZEaH09aWZ6sLga8dOja-UIp3Iz3DSOAb4bcH4"
      }
    }
  }
}
2
It seems there is a typo in your indexing rules: $tid instead of $uid - Renaud Tarnec
Just sec, I will attach. And did you face this problem before? - Stanislav Sergeev
@Stanislas Sergeev Note that you can easily get a text export (JSON) of your Database by clicking the Export button in your Firebase Database console - Renaud Tarnec
But there are too much data - Stanislav Sergeev
@Stanislas Sergeev You may delete most of the data and keep a set of "meaningfull" nodes showing the structure - Renaud Tarnec

2 Answers

2
votes

I had same issue before and it is related with your Location. Please try to use VPN.

0
votes

So, based on the comments and your database structure:

Firstly, I don't understand what the following piece of rule is for?

 "threads": {
    "$tid": {
      "messages": {
        ".indexOn": ["read", "date"]
      }
    }
  }

Secondly, can you try just with the basic rules:

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

Thirdly, are you sure you are looged in? What do you get if you do

var uid = info.uid;
console.log(uid);

PS: note that the method you are using is apparently going to be deprecated, see https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithEmailAndPassword


EDIT: This is working, I've reproduced your case. Note the return and the data.val().

firebase.auth().signInWithEmailAndPassword("[email protected]", "****")
    .then(function (info) {
        var uid = info.uid;
        console.log(uid);
        return firebase.database().ref('users/' + uid + '/userDetails').once('value');
    })
    .then(function (data) {
        console.log(data.val());
    })
    .catch(function (error) {
        alert(error.message);
    });