esta es mi base de datos en tiempo real
Uso la base de datos en tiempo real de firebase, uso las reglas de la siguiente manera:
{ "rules": { "$uid": { ".read":"$uid === auth.uid", ".write": false } } }cuando uso las reglas del patio de recreo como se muestra a continuación, funciona, aquí logré leer los datos 121212 anteriores.
Este es mi fragmento de código.
<script> // Import the functions you need from the SDKs you need import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.11/firebase-app.js"; import { getDatabase, ref, onValue, } from "https://www.gstatic.com/firebasejs/9.6.11/firebase-database.js"; import { getAuth, signInWithCustomToken, createCustomToken } from "https://www.gstatic.com/firebasejs/9.6.11/firebase-auth.js"; // TODO: Add SDKs for Firebase products that you want to use // https://firebase.google.com/docs/web/setup#available-libraries // Your web app's Firebase configuration // For Firebase JS SDK v7.20.0 and later, measurementId is optional const firebaseConfig = { apiKey: "", authDomain: "", databaseURL: "", projectId: "", storageBucket: "", messagingSenderId: "", appId: "", measurementId: "" }; // Initialize Firebase const app = initializeApp(firebaseConfig); </script>El problema es que quiero leer datos 121212 a través de javascript.
El auth.uid es el UID del usuario que inició sesión con Firebase Authentication en su aplicación web que solicita los datos. Primero tendrás que iniciar sesión:
import { getAuth, signInWithEmailAndPassword, } from "https://www.gstatic.com/firebasejs/9.6.11/firebase-auth.js"; const app = initializeApp(firebaseConfig); const auth = getAuth(app); signInWithEmailAndPassword(auth, "user@domain.tld", "password").then((user) => { // read data using Firebase Realtime Database SDK })Firebase ha publicado recientemente muchos tutoriales en su canal de YouTube que serían útiles:
Si no desea que el usuario tenga que proporcionar credenciales, pero desea que solo pueda acceder a sus propios datos, considere usar la autenticación anónima de Firebase, donde iniciar sesión es tan fácil como:
import { getAuth, signInAnonymously } from "firebase/auth"; const auth = getAuth(); await signInAnonymously(auth);