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

时间:2019-06-13 18:17:45

标签: ios swift google-cloud-firestore

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

我的应用程序(iOs,Swift)配置为项目A作为默认的Firebase项目。我能够将项目B设置为辅助Firebase实例,针对项目B成功验证用户身份,并可以访问项目B中的Firestore数据库(进行身份验证控制,在规则中检查request.auth.uid)。到目前为止一切顺利。

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

我尝试遵循以下指南:https://firebase.googleblog.com/2016/12/working-with-multiple-firebase-projects-in-an-android-app.html,但是由于看起来像Firestore错误(请参阅https://github.com/firebase/FirebaseUI-iOS/issues/670)而陷入使Google登录方法正常工作的问题

我的项目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;
    }
  }
}

我想以类似的方式设置项目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

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

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

0 个答案:

没有答案