如何使用 javascript 在 firebase 实时数据库中使用自定义 uid 进行身份验证?
how to authenticate with custom uid in firebase realtime database using javascript?
这是我的实时数据库
我使用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数据。
您的网络应用中的 auth.uid
is UID of user logged in with Firebase Authentication 请求数据。首先,您必须让他们登录:
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 最近在他们的 YouTube 频道上发布了许多有用的教程:
如果您不希望用户必须提供凭据,但仍然希望只能访问他们自己的数据,请考虑使用 Firebase 的 anonymous authentication,其中登录非常简单:
import { getAuth, signInAnonymously } from "firebase/auth";
const auth = getAuth();
await signInAnonymously(auth);
这是我的实时数据库
我使用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数据。
您的网络应用中的 auth.uid
is UID of user logged in with Firebase Authentication 请求数据。首先,您必须让他们登录:
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 最近在他们的 YouTube 频道上发布了许多有用的教程:
如果您不希望用户必须提供凭据,但仍然希望只能访问他们自己的数据,请考虑使用 Firebase 的 anonymous authentication,其中登录非常简单:
import { getAuth, signInAnonymously } from "firebase/auth";
const auth = getAuth();
await signInAnonymously(auth);