匿名身份验证
您可以使用 Firebase Authentication 创建和使用临时匿名账号来进行 Firebase 身份验证。借助临时匿名账号,尚未注册应用的用户也可以使用受安全规则保护的数据。如果匿名用户决定注册您的应用,您可以将匿名用户的登录凭据与匿名账号关联,这样他们就可以在以后的会话中继续使用其受保护的数据。
准备工作
-
使用 Swift Package Manager 安装和管理 Firebase 依赖项。
-
在 Xcode 中打开您的应用项目,依次点击 File(文件)> Add Packages(添加软件包)。
- 出现提示时,添加 Firebase Apple 平台 SDK 代码库:
https://github.com/firebase/firebase-ios-sdk.git
- 选择 Firebase Authentication 库。
- 将
-ObjC标志添加到目标 build 设置的“其他链接器标志”部分。 - 完成之后,Xcode 将会自动开始在后台解析和下载您的依赖项。
- 如果您尚未将您的应用与 Firebase 项目关联,请在 Firebase 控制台中进行关联。
- 启用匿名身份验证:
- 在 Firebase 控制台中,打开 Auth 部分。
- 在 Sign-in Methods(登录方法)页面中,启用匿名登录方法。
- 可选:如果您已将项目升级为采用 Firebase Authentication with Identity Platform,则可以启用自动清理。启用此设置后,系统会自动删除超过 30 天的匿名账号。在启用了自动清理的项目中,匿名身份验证将不再计入用量限额或结算配额。请参阅自动清理。
匿名进行 Firebase 身份验证
当某个未登录的用户使用需要进行 Firebase 身份验证的应用功能时,您可以按以下步骤操作,让用户匿名登录:
- 在
UIApplicationDelegate中导入FirebaseCore模块,以及您的应用委托 (app delegate) 使用的所有其他 Firebase 模块。 例如,使用 Cloud Firestore 和 Authentication:
#### SwiftUI
import SwiftUI
import FirebaseCore
import FirebaseFirestore
import FirebaseAuth
// ...
#### Swift
import FirebaseCore
import FirebaseFirestore
import FirebaseAuth
// ...
#### Objective-C
@import FirebaseCore;
@import FirebaseFirestore;
@import FirebaseAuth;
// ...
2. 在应用委托的 application(_:didFinishLaunchingWithOptions:) 方法中配置一个 FirebaseApp 共享实例:
#### SwiftUI
// Use Firebase library to configure APIs
FirebaseApp.configure()
#### Swift
// Use Firebase library to configure APIs
FirebaseApp.configure()
#### Objective-C
// Use Firebase library to configure APIs
[FIRApp configure];
3. 如果您使用的是 SwiftUI,则必须创建应用委托并通过 UIApplicationDelegateAdaptor 或 NSApplicationDelegateAdaptor 将其附加到 App 结构体。您还必须停用应用委托调配。如需了解详情,请参阅 SwiftUI 说明。
#### SwiftUI
``` @main struct YourApp: App { // register app delegate for Firebase setup @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
NavigationView {
ContentView()
}
}
}
}
``
4. 调用signInAnonymouslyWithCompletion:` 方法:
#### Swift
Auth.auth().signInAnonymously { authResult, error in
// ...
}
#### Objective-C
[[FIRAuth auth] signInAnonymouslyWithCompletion:^(FIRAuthDataResult * _Nullable authResult,
NSError * _Nullable error) {
// ...
}];
5. 如果 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
FIRAuthCredential *credential =
[FIRGoogleAuthProvider credentialWithIDToken:result.user.idToken.tokenString
accessToken:result.user.accessToken.tokenString];
##### 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];
3. 将 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 Authentication with Identity Platform,则可以在 Firebase 控制台中启用自动清理。启用此功能即表示您允许 Firebase 自动删除超过 30 天的匿名账号。在启用了自动清理的项目中,匿名身份验证不会计入用量限额或结算配额。
- 启用自动清理后创建的任何匿名账号都可能会在创建 30 天后随时自动删除。
- 现有匿名账号将在启用自动清理 30 天后开始自动删除。
- 如果您关闭了自动清理,计划删除的匿名账号仍将按照计划删除。
- 如果您通过将匿名账号与任何登录方法相关联来“升级”该账号,则系统不会自动删除该账号。
如果您想在启用此功能前了解多少用户会受到影响,并且您已将项目升级为采用 Firebase Authentication with Identity Platform,则可以在 Cloud Logging 中按 is_anon 进行过滤。
后续步骤
在用户可以进行 Firebase 身份验证后,您可以使用 Firebase 规则控制用户对您 Firebase 数据库中的数据的访问权限。
