您可以用 Firebase Authentication 创建和使用临时匿名帐号来进行 Firebase 身份验证。借助临时匿名帐号,尚未注册您应用的用户也可以使用受安全规则保护的数据。如果匿名用户决定注册您的应用,您可以将他们的登录凭据与匿名帐号关联,这样他们就可以在以后的会话中继续使用其受保护的数据。
准备工作
使用 Swift Package Manager 安装和管理 Firebase 依赖项。
- 在 Xcode 中打开您的应用项目,依次转到 File > Add Packages。
- 出现提示时,添加 Firebase Apple 平台 SDK 代码库:
- 选择 Firebase Authentication 库。
- 完成之后,Xcode 将会自动开始在后台解析和下载您的依赖项。
https://github.com/firebase/firebase-ios-sdk
接下来,执行一些配置步骤:
- 如果您尚未将您的应用与 Firebase 项目关联,请在 Firebase 控制台中进行关联。
- 启用匿名身份验证:
- 在 Firebase 控制台中,打开身份验证部分。
- 在登录方法页面中,启用匿名登录方法。
匿名进行 Firebase 身份验证
当某个未登录的用户使用需要进行 Firebase 身份验证的应用功能时,您可以按以下步骤操作,让用户匿名登录:
- 在
UIApplicationDelegate
中导入FirebaseCore
模块,以及您的应用委托 (app delegate) 使用的所有其他 Firebase 模块。 例如,如需使用 Cloud Firestore 和 Authentication,请执行以下操作:Swift
import FirebaseCore import FirebaseFirestore import FirebaseAuth // ...
Objective-C
@import FirebaseCore; @import FirebaseFirestore; @import FirebaseAuth; // ...
- 配置一个
FirebaseApp
共享实例(通常在应用的application:didFinishLaunchingWithOptions:
方法中配置):Swift
// Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
- 调用
signInAnonymouslyWithCompletion:
方法:Swift
Auth.auth().signInAnonymously { authResult, error in // ... }
Objective-C
[[FIRAuth auth] signInAnonymouslyWithCompletion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { // ... }];
- 如果
signInAnonymouslyWithCompletion:
方法顺利完成,未出现任何错误,您可以从FIRAuthDataResult
对象中获取匿名用户的帐号数据:Swift
guard let user = authResult?.user else { return } let isAnonymous = user.isAnonymous // true let uid = user.uid
Objective-C
FIRUser *user = authResult.user; BOOL isAnonymous = user.anonymous; // YES NSString *uid = user.uid;
将匿名帐号转成永久帐号
当匿名用户注册您的应用时,您可能需要允许其使用新帐号继续之前的操作。例如,您可能需要将用户在注册之前添加到购物车中的商品移到其新帐号的购物车中。为此,请完成以下步骤:
- 当用户注册时,可按照用户身份验证提供方的登录流程逐步进行,直到需要调用某一
FIRAuth.signInWith
方法时停止。例如,获取用户的 Google ID 令牌、Facebook 访问令牌或电子邮件地址和密码。 为新的身份验证提供方获取
FIRAuthCredential
:Google 登录
Swift
guard let authentication = user?.authentication, let idToken = authentication.idToken else { return } let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: authentication.accessToken)
Objective-C
GIDAuthentication *authentication = user.authentication; FIRAuthCredential *credential = [FIRGoogleAuthProvider credentialWithIDToken:authentication.idToken accessToken:authentication.accessToken];
Facebook 登录
Swift
let credential = FacebookAuthProvider .credential(withAccessToken: AccessToken.current!.tokenString)
Objective-C
FIRAuthCredential *credential = [FIRFacebookAuthProvider credentialWithAccessToken:[FBSDKAccessToken currentAccessToken].tokenString];
电子邮件地址/密码登录
Swift
let credential = EmailAuthProvider.credential(withEmail: email, password: password)
Objective-C
FIRAuthCredential *credential = [FIREmailAuthProvider credentialWithEmail:email password:password];
将
FIRAuthCredential
对象传递给登录用户的linkWithCredential:completion:
方法:Swift
user.link(with: credential) { authResult, error in // ... } }
Objective-C
[[FIRAuth auth].currentUser linkWithCredential:credential completion:^(FIRAuthDataResult *result, NSError *_Nullable error) { // ... }];
如果对 linkWithCredential:completion:
的调用成功,用户的新帐号就可以访问匿名帐号的 Firebase 数据了。
后续步骤
在用户可以进行 Firebase 身份验证后,您可以使用 Firebase 规则控制用户对 Firebase 数据库的访问权限。