Firebase身份验证:在Swift中链接多个帐户 [英] Firebase authentication: linking multiple accounts in Swift

查看:170
本文介绍了Firebase身份验证:在Swift中链接多个帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Facebook,Google&还有我的iOS应用程序设置了Firebase身份验证。电子邮件/密码登录,这一切工作正常。只有当用户想访问我的应用程序的高优先级部分(即我不需要用户登录开始使用应用程序)时,才会发生此身份验证。



在应用程序启动时,我在后台匿名签名用户,这也正常工作。



我已经阅读了文档,但我正在努力了解使我能够将匿名帐户链接到登录的Facebook /电子邮件所需的代码帐户在以下流程中:


  • 新用户打开应用程序

  • 用户在后台匿名登录(新建user.uidA)
  • 在Firebase实时数据库中针对匿名用户存储的低优先级数据

  • 用户击中高优先级区域所以需要进行身份验证

  • 用户使用Facebook登录(新建user.uidB)
  • 以前的user.uidA需要被链接到user.uidB



我的方法目前看起来像这样:

  func signupWithFacebook (){
//跟踪匿名用户以后连接
let prevUser = FIRAuth.auth()?currentUser

FBSDKLoginManager()。logInWithReadPermissions([public_profile,如果让token = result?.token?.tokenString {
let credential = FIRFacebookAuthProvider.credentialWithAccessToken(token)
$,则可以从ViewController:self){(result,error) b $ b FIRAuth.auth()?。signInWithCredential(证书,完成:{(user,error)in
if user!= nil&&错误== nil {
//成功
self.success?(user:user!)
dispatch_async(dispatch_get_main_queue(),{
self.dismissViewControllerAnimated(true,completion:nil )
})

}
})
}
}
}
pre

$ p









p> 更新:
我意识到我对应用程序逻辑感到困惑,因为用户在测试过程中被创建。而不是为上述场景(通过Facebook和其他匿名身份验证)创建2个单独的用户,所发生的只是原始匿名user.uidA被链接到某些Facebook身份验证凭证。在Firebase控制台中,匿名用户名从匿名用户变为匿名用户,并在其旁边添加了Facebook徽标。



这就是我的工作方式: p>

  func signupWithFacebook(){
FBSDKLoginManager()。logInWithReadPermissions([public_profile,email],fromViewController:self ){(result,error)in
if let token = result?.token?.tokenString {
let credential = FIRFacebookAuthProvider.credentialWithAccessToken(token)

FIRAuth.auth() ?.currentUser!.linkWithCredential(凭证){(user,error)in
if user!= nil&&错误== nil {
//成功
self.success?(user:user!)
dispatch_async(dispatch_get_main_queue(),{
self.dismissViewControllerAnimated(true,completion:nil )
} else
} else {
print(linkWithCredential error:,error)
}
}
}
}


$ b $ div

解决方案

所以你的代码遵循前两个步骤在链接。但是,文档明确说不要调用 signInWithCredential ,而是调用

  FIRAuth .auth()?。currentUser.linkWithCredential(凭证){(用户,错误)在
// ...
}

从Facebook的SDK获取您的凭证后。



从链接引用: linkWithCredential:完成:成功后,用户的新帐户可以访问匿名帐户的Firebase数据。


I've set up Firebase authentication for my iOS app using Facebook, Google & email/password sign in and it's all working fine. This authentication only happens when the user wants to access high-priority parts of my app (i.e. I don't require users to sign in to start using the app).

On app start up, I sign users in anonymously in the background and that's working fine too.

I've read the documentation but I'm struggling to understand the code required to enable me to link an anonymous account to a Facebook/email signed in account in the following flow:

  • new user opens app
  • user signed in anonymously in the background (new user.uid "A" created)
  • low priority data stored against anonymous user in Firebase realtime DB
  • user hits a high-priority area so needs to authenticate
  • user signs in using Facebook (new user.uid "B" created)
  • previous user.uid "A" needs to be linked to user.uid "B"

My method currently looks like this:

func signupWithFacebook(){
    // track the anonymous user to link later
    let prevUser = FIRAuth.auth()?.currentUser

    FBSDKLoginManager().logInWithReadPermissions(["public_profile", "email"], fromViewController: self) { (result, error) in
        if let token = result?.token?.tokenString {
            let credential = FIRFacebookAuthProvider.credentialWithAccessToken(token)

            FIRAuth.auth()?.signInWithCredential(credential, completion: { (user, error) in
                if user != nil && error == nil {
                    // Success
                    self.success?(user: user!)
                    dispatch_async(dispatch_get_main_queue(), {
                        self.dismissViewControllerAnimated(true, completion: nil)
                    })

                }
            })
        }
    }
}

Any pointers to remove the confusion would be great.


UPDATE: I've realised I was confused about the app logic because of users being created during testing. Instead of 2 separate users being created for the above scenario (one authenticated via Facebook and another anonymously), all that happens is that the original anonymous user.uid "A" is "linked" to some Facebook authentication credentials. In the Firebase console this is shown by the anonymous uid changing from anonymous to one with the Facebook logo next to it.

This is what my working method looks like:

func signupWithFacebook(){
    FBSDKLoginManager().logInWithReadPermissions(["public_profile", "email"], fromViewController: self) { (result, error) in
        if let token = result?.token?.tokenString {
            let credential = FIRFacebookAuthProvider.credentialWithAccessToken(token)

            FIRAuth.auth()?.currentUser!.linkWithCredential(credential) { (user, error) in
                if user != nil && error == nil {
                    // Success
                    self.success?(user: user!)
                    dispatch_async(dispatch_get_main_queue(), {
                        self.dismissViewControllerAnimated(true, completion: nil)
                    })
                } else {
                    print("linkWithCredential error:", error)
                }
            }
        }
    }
}

解决方案

So your code follows the first 2 steps in this link. But the documentation explicity says not to call signInWithCredential but instead call

FIRAuth.auth()?.currentUser.linkWithCredential(credential) { (user, error) in
  // ...
}

After getting your credential from Facebook's SDK.

Quote from link: "If the call to linkWithCredential:completion: succeeds, the user's new account can access the anonymous account's Firebase data."

这篇关于Firebase身份验证:在Swift中链接多个帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆