如何使用来自另一个 Firestore 项目的身份验证对 Firestore 数据库进行身份验证

Bru*_*uce 5 ios swift google-cloud-firestore

我有 2 个 firebase 项目,项目 A 和项目 B。在项目 B 中设置了身份验证,并填充了注册用户(电子邮件和密码登录方法)。

我的应用程序(iOs、Swift)配置了项目 A 作为默认的 Firebase 项目。我能够将项目 B 设置为辅助 Firebase 实例,针对项目 B 成功验证用户身份,并访问项目 B 中的 Firestore 数据库(验证受控,检查规则中的 request.auth.uid)。到现在为止还挺好。

我可以访问项目 A 中的(默认)Firestore 数据库,只要 Firestore 数据库规则保持打开状态(不检查 request.auth.uid)。当我添加规则来检查 request.auth.uid 时,我被拒绝访问,我希望是因为我针对项目 B 而不是项目 A 进行了身份验证。

我尝试按照本指南进行操作:https : //firebase.googleblog.com/2016/12/working-with-multiple-firebase-projects-in-an-android-app.html,但在获取 Google 登录方法时遇到了问题由于看起来像 firestore 错误而工作(请参阅https://github.com/firebase/FirebaseUI-iOS/issues/670

我的项目 B Firestore 规则设置如下:

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth.uid == userId;
      allow create: if request.auth.uid != null;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我想以类似的方式设置我的项目 A Firestore 规则(限制对来自项目 B 的登录用户的访问)

作为参考,这是我创建辅助 firebase 实例的方式:

//Setup the default firebase app 
FirebaseApp.configure()

//
//Set up a secondary firebase app that we want to authenticate against
//Acutal ID's etc altered for privacy reasons
//
let secondaryOptions = FirebaseOptions(googleAppID: "myAppId", gcmSenderID: "mySenderID")
secondaryOptions.bundleID = "myBundleID"
secondaryOptions.apiKey = "myAPIKey"
secondaryOptions.clientID = "myClientID"
secondaryOptions.databaseURL = "myDbUrl"
secondaryOptions.storageBucket = "myBucket"

// Configure an alternative FIRApp.
FirebaseApp.configure(name: "secondary", options: secondaryOptions)

// Retrieve a previous created named app.
guard let secondary = FirebaseApp.app(name: "secondary")
  else { assert(false, "Could not retrieve secondary app") }

// Retrieve the auth for the secondary app
let secondaryAuth = Auth.auth(app: secondary)
authUI = FUIAuth(uiWith: secondaryAuth)

// We need to adopt a FUIAuthDelegate protocol to receive callback
authUI?.delegate = self

//Set up providers
let providers: [FUIAuthProvider] = [
  FUIEmailAuth(authAuthUI: authUI, signInMethod: EmailPasswordAuthSignInMethod, forceSameDevice: false, allowNewEmailAccounts: true, actionCodeSetting: ActionCodeSettings())
]
authUI?.providers = providers

//After this, I can log users in using authUI, and access a db on secondary
//I can also access a db on the default Firebase instance, I just can't figure out how to authenticate using my secondary instance
Run Code Online (Sandbox Code Playgroud)

我不想让数据库 A 没有规则。我不希望用户必须创建 2 个帐户或登录两次。

有什么方法可以使用来自 Firebase 实例 B 的已通过身份验证的用户对 Firebase 实例 A 进行身份验证?