你有想过 Line、KKBOX 等 App 上面的「与外部帐号连结」是怎麽做到的吗
▲ Line
▲ KKBOX
其实都是用到类似今天这篇要分享的方法-与其他帐号连结 (Link To Other Account)
首先先到 Firebase Auth 官方开发文件
点击「Link Multiple Auth Providers to an Account on iOS」
在文件中可以看到,如果要将其他身份验证提供者与单一帐号做连结的话
会需要用到 Credential (凭证) 这个东西,那这个东西要怎麽生出来呢,其实很简单
因为先前在分享 Google 帐号、Facebook 帐号、Apple ID 登入的时候就已经有产生了
【在 iOS 开发路上的大小事-Day18】透过 Firebase 来管理使用者 (Sign in with Google 篇) Part2
【在 iOS 开发路上的大小事-Day20】透过 Firebase 来管理使用者 (Sign in with Facebook 篇) Part2
【在 iOS 开发路上的大小事-Day22】透过 Firebase 来管理使用者 (Sign in with Apple 篇) Part2
// Sign in with Google
let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: authentication.accessToken)
// Sign in with Facebook
let credential = FacebookAuthProvider.credential(withAccessToken: accessToken.tokenString)
// Sign in with Apple
let credential = OAuthProvider.credential(withProviderID: "apple.com", idToken: idTokenString, rawNonce: nonce)
接下来就来透过 Credential 来将多种 OAuth 身份验证提供者通通连结在一起
下面就以 Sign in with Google 来做示范,其他的登入方式做法都是大同小异的~
先前分享的 Sign in with Google 与 Firebase Auth 连接的部分如下
func firebaseSignInWithGoogle(credential: AuthCredential) {
Auth.auth().signIn(with: credential) { authResult, error in
guard error == nil else {
CustomFunc.customAlert(title: "", message: "\(String(describing: error!.localizedDescription))", vc: self, actionHandler: nil)
return
}
CustomFunc.customAlert(title: "登入成功!", message: "", vc: self, actionHandler: self.getFirebaseUserInfo)
}
}
我们要将它改成下面这样,透过 Credential 来与其他 OAuth 身份验证方式连结在一起
func firebaseSignInWithGoogle(credential: AuthCredential) {
if let user = Auth.auth().currentUser {
user.link(with: credential) { authResult, error in
if let error = error {
CustomFunc.customAlert(title: "", message: "\(String(describing: error.localizedDescription))", vc: self, actionHandler: nil)
return
}
CustomFunc.customAlert(title: "", message: "与其他登入方式的帐号连结成功!", vc: self, actionHandler: nil)
}
} else {
Auth.auth().signIn(with: credential) { authResult, error in
guard error == nil else {
CustomFunc.customAlert(title: "", message: "\(String(describing: error!.localizedDescription))", vc: self, actionHandler: nil)
return
}
CustomFunc.customAlert(title: "登入成功!", message: "", vc: self, actionHandler: self.getFirebaseUserInfo)
}
}
}
假设有一天突然改变心意,像是变心的人一样
不想再与其进行连结的话,可以透过下面的方法来解除 (切八段)
let providerID = Auth.auth().currentUser?.providerID
Auth.auth().currentUser?.unlink(fromProvider: providerID!) { user, error in
if let error = error {
CustomFunc.customAlert(title: "", message: "\(String(describing: error.localizedDescription))", vc: self, actionHandler: nil)
return
}
CustomFunc.customAlert(title: "", message: "已取消与其他登入方式的帐号连结!", vc: self, actionHandler: nil)
}
以上就是透过 Firebase Auth 来与多个 OAuth 身份验证提供者进行连结和解除连结的方法~
参考资料:
<<: Day19 [PM杂技]word大型文件产制 -合并文件
一般删除的话资料就永远消失了,有时候我们为了避免某些重要资料不小心删除後再也无法救回的情况,会让资料...
选择具有 GPU 的 EC2 并完成配置-Day 02 需要配置一台电脑来处理接下来所有的服务,因为...
今天我们来聊聊生成对抗网路 (Generative Adversarial Network, GAN...
Objective-C中的变数分为储存基本资料型态的数值(value)以及指标(pointer) P...
我有一台对外服务的 MS-SQL 资料库主机,最近从事件检视器里面发现一直有不明的攻击者利用暴力破解...