我是 Firebase 服务的新手,但我已经完成了负责 Firebase 实时数据库交互的 iOS 应用程序的代码。
现在我想确保我的应用安全并使用 Firebase 规则保护它。有一个问题,我正在为用户使用我自己的身份验证,因此我不使用 Firebase Auth。
所以问题是如何在没有 Firebase Auth 的情况下使用 Firebase 规则保护我的数据库。
最佳答案
注意:据我所知,无法在 Firebase 中直接使用您的自定义身份验证系统。
假设:您有一个集成了 Firebase Admin SDK(可以/已经)的身份验证服务器。
您需要创建自定义 token 才能在数据库/存储中使用您的身份验证:
https://firebase.google.com/docs/auth/admin/create-custom-tokens
Once authenticated, this identity will be used when accessing other Firebase services, such as the Firebase Realtime Database and Cloud Storage. Furthermore, the contents of the JWT will be available in the auth object in your Firebase Realtime Database Security Rules and the request.auth object in your Cloud Storage Security Rules.
从上层链接省略 Java 和 Python
在服务器中:
// Step 1: Your client has sent the credentials.
// Step 2: Fetch the client's unique id, and create a custom token with the Admin SDK.
var uid = "some-uid";
admin.auth().createCustomToken(uid)
.then(function(customToken) {
// Send token back to client
})
.catch(function(error) {
console.log("Error creating custom token:", error);
});
然后在iOS部分:
// Step 1: Login with your own authentication system.
// Step 2: Send your credentials to your server, and fetch the customToken.
// Step 3: Sign in with FIRAuth:
[[FIRAuth auth] signInWithCustomToken:customToken
completion:^(FIRUser *_Nullable user, NSError *_Nullable error) {
// ...
}];
关于ios - 不使用 Firebase 身份验证的实时数据库的 Firebase 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45586418/