利用 Firebase Authentication,您可以允许用户通过一种或多种方式登录到您的应用,其中包括电子邮件地址和密码登录以及联合身份提供方(如 Google 登录和 Facebook 登录)。本教程将向您展示如何为自己的应用添加电子邮件地址和密码登录功能,以开始使用 Firebase Authentication。
将您的应用关联至 Firebase
- 安装 Firebase SDK。
- 在 Firebase 控制台中,将您的应用添加到您的 Firebase 项目。
将 Firebase Authentication 添加到您的应用
使用 Swift Package Manager 安装和管理 Firebase 依赖项。
- 在 Xcode 中打开您的应用项目,依次点击 File(文件)> Add Packages(添加软件包)。
- 出现提示时,添加 Firebase Apple 平台 SDK 代码库:
- 选择 Firebase Authentication 库。
- 将
-ObjC标志添加到目标 build 设置的“其他链接器标志”部分。 - 完成之后,Xcode 将会自动开始在后台解析和下载您的依赖项。
https://github.com/firebase/firebase-ios-sdk.git
(可选)使用 Firebase Local Emulator Suite 进行原型设计和测试
在介绍应用如何对用户进行身份验证之前,我们先介绍一套可用于对 Authentication 功能进行原型设计和测试的工具,即 Firebase Local Emulator Suite。如果您正要从几种身份验证方法和数个提供方中做出选择、对使用 Authentication 和 Firebase Security Rules 安全规则的公共和私有数据尝试不同的数据模型,或者想要对登录界面进行原型设计,那么无需实际部署即可在本地进行上述工作是非常理想的。
Authentication 模拟器是 Local Emulator Suite 的一部分,通过该模拟器,您的应用可以与模拟的数据库内容和配置进行交互,并可视需要与模拟的项目资源(函数、其他数据库和安全规则)进行交互。
如需使用 Authentication 模拟器,只需完成几个步骤:
- 向应用的测试配置添加一行代码以连接到模拟器。
- 从本地项目的根目录运行
firebase emulators:start。 - 使用 Local Emulator Suite 界面进行交互式原型设计,或使用 Authentication 模拟器 REST API 进行非交互式测试。
如需查看详细指南,请参阅将您的应用连接到 Authentication 模拟器。如需了解详情,请参阅 Local Emulator Suite 简介。
接下来,我们来看看如何对用户进行身份验证。
初始化 Firebase SDK
在您的应用委托中,首先导入 Firebase SDK:
Swift
import FirebaseCore Objective-C
@import FirebaseCore; 然后,在 application:didFinishLaunchingWithOptions: 方法中,初始化 FirebaseApp 对象:
Swift
// Use Firebase library to configure APIs FirebaseApp.configure() Objective-C
// Use Firebase library to configure APIs [FIRApp configure]; 监听身份验证状态
对于需要已登录用户信息的每个应用视图,将监听器附加到 FIRAuth 对象。每当用户的登录状态发生变化时,系统都会调用此监听器。
在视图控制器的 viewWillAppear 方法中附加此监听器:
Swift
handle = Auth.auth().addStateDidChangeListener { auth, user in // ... } Objective-C
self.handle = [[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth, FIRUser *_Nullable user) { // ... }]; 在视图控制器的 viewWillDisappear 方法中分离此监听器:
Swift
Auth.auth().removeStateDidChangeListener(handle!) Objective-C
[[FIRAuth auth] removeAuthStateDidChangeListener:_handle]; 注册新用户
创建一个表单,以便新用户使用自己的电子邮件地址和密码注册您的应用。当用户填好该表单后,验证用户提供的电子邮件地址和密码,然后将其传递给 createUser 方法:
Swift
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in // ... } Objective-C
[[FIRAuth auth] createUserWithEmail:email password:password completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { // ... }]; 登录现有用户
创建一个表单,以便现有用户使用自己的电子邮件地址和密码登录。当用户填好该表单后,调用 signIn 方法:
Swift
Auth.auth().signIn(withEmail: email, password: password) { [weak self] authResult, error in guard let strongSelf = self else { return } // ... } Objective-C
[[FIRAuth auth] signInWithEmail:self->_emailField.text password:self->_passwordField.text completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { // ... }]; 获取用户信息
在用户成功登录后,您可以获取有关用户的信息。例如,在您的身份验证状态监听器中:
Swift
if let user = user { // The user's ID, unique to the Firebase project. // Do NOT use this value to authenticate with your backend server, // if you have one. Use getTokenWithCompletion:completion: instead. let uid = user.uid let email = user.email let photoURL = user.photoURL var multiFactorString = "MultiFactor: " for info in user.multiFactor.enrolledFactors { multiFactorString += info.displayName ?? "[DispayName]" multiFactorString += " " } // ... } Objective-C
if (user) { // The user's ID, unique to the Firebase project. // Do NOT use this value to authenticate with your backend server, // if you have one. Use getTokenWithCompletion:completion: instead. NSString *email = user.email; NSString *uid = user.uid; NSMutableString *multiFactorString = [NSMutableString stringWithFormat:@"MultiFactor: "]; for (FIRMultiFactorInfo *info in user.multiFactor.enrolledFactors) { [multiFactorString appendString:info.displayName]; [multiFactorString appendString:@" "]; } NSURL *photoURL = user.photoURL; // ... } 后续步骤
了解如何添加对其他身份提供方和匿名访客账号的支持: