如何使用JavaScript在Firebase实时数据库中使用自定义UID进行身份验证?

发布于 2025-01-24 09:50:04 字数 1678 浏览 0 评论 0 原文

这是我的实时数据库

“在此处输入图像描述”

我使用firebase实时数据库,我使用以下规则:

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

当我在下面使用的操场规则时,我在这里设法读取上面的121212数据。

这是我的代码段。

<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>

问题是,我想通过JavaScript读取121212数据。

this is my realtime database

enter image description here

I use firebase realtime database, I use rules as below:

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

when I use the playground rules as below it works, here I managed to read the 121212 data above.

enter image description here

This is my code snippet.

<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>

The problem is, I want to read 121212 data via javascript.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白色秋天 2025-01-31 09:50:04

auth> auth.uid 用户使用 firebase身份验证在请求数据的网络应用中,用户登录。首先,您必须在其中签名:

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, "[email protected]", "password").then((user) => {
  // read data using Firebase Realtime Database SDK
})

Firebase最近在其YouTube频道上发布了许多教程,这些教程很有用:

The auth.uid is UID of user logged in with Firebase Authentication in your web app requesting the data. First you'll have to sign them in:

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, "[email protected]", "password").then((user) => {
  // read data using Firebase Realtime Database SDK
})

Firebase has recently posted many tutorials on their YouTube channel that would be useful:

如梦 2025-01-31 09:50:04

如果您不希望用户提供凭据,但仍然希望只能访问自己的数据,请考虑使用Firebase的匿名身份验证签名在其中很容易:

import { getAuth, signInAnonymously } from "firebase/auth";

const auth = getAuth();
await signInAnonymously(auth);

If you don't want the user to have to provide credentials, but still want then to only be able to access their own data, consider using Firebase's anonymous authentication where signing in is as easy as:

import { getAuth, signInAnonymously } from "firebase/auth";

const auth = getAuth();
await signInAnonymously(auth);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文