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

问题描述 投票:4回答:1

我有两个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,但是由于看起来像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进行身份验证?

ios swift google-cloud-firestore
1个回答
0
投票

我也有同样的问题。您是否找到解决方案?

© www.soinside.com 2019 - 2024. All rights reserved.