我已经在数据库安全规则上苦苦挣扎了一段时间。特别是在身份验证操作方面,例如
".write": "auth.uid === $uid"
。
今天我意识到 Firebase SDK 只会在默认应用程序的 RTDB 中记录用户,因此不会在其他数据库交换期间发送身份验证。 我在多个数据库的身份验证过程中是否遗漏了什么?
这是 Firebase 配置:
firebaseConfig: {
apiKey: "xxx",
appId: "xxx"
authDomain: "xxx",
databaseURL: "https://my-default-rtdb.europe-west1.firebasedatabase.app/",
projectId: "xxx",
storageBucket: "xxx",
...
},
firebaseAnotherConfig: {
databaseURL: "https://another-rtdb-instance.europe-west1.firebasedatabase.app/",
storageBucket: "xxx", // same as firebaseConfig
},
firebaseThelastConfig: {
databaseURL: "https://the-last-rtdb-instance.europe-west1.firebasedatabase.app/",
storageBucket: "xxx", // same as firebaseConfig
}
这里是初始化
initializeApp(firebaseConfig);
initializeApp(firebaseAnotherConfig, 'another');
initializeApp(firebaseThelastConfig, 'thelast');
然后身份验证管理通过 getAuh() 函数进行工作,因此在“firebaseConfig”的默认应用程序中。
所有应用程序在验证之前都已初始化。 所有数据交换都与所有数据库正常工作。
谷歌控制台中的测试在所有数据库中都运行良好。 但是,要知道,rtdb 规则“auth.uid !== null”仅适用于默认 rtdb。
我注意到 firebase/app 中检索所有应用程序的以下函数:“getApps()”。但我无法让它与 Firebase“登录..”功能一起使用。
当您登录 Firebase 身份验证时,您将登录到整个 项目 - 而不是特定数据库。
如果您想要连接到与传递给
initializeApp
的配置数据中包含的数据库不同的数据库,您可以将该数据库的 URL 传递给 getDatabase
调用。
举个例子:
let app = initializeApp(firebaseConfig);
let db1 = getDatabase(app); // this gets you the configured database
let db2 = getDatabase(app, "url of other database");