如何使用JavaScript在Firebase实时数据库中使用自定义UID进行身份验证?
这是我的实时数据库
我使用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数据。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
auth> auth.uid
用户使用 firebase身份验证在请求数据的网络应用中,用户登录。首先,您必须在其中签名: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:Firebase has recently posted many tutorials on their YouTube channel that would be useful:
如果您不希望用户提供凭据,但仍然希望只能访问自己的数据,请考虑使用Firebase的匿名身份验证签名在其中很容易:
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: