利用 Firebase Authentication,您可以允许用户通过一种或多种方式登录到您的应用,其中包括电子邮件地址和密码登录以及联合身份提供方(如 Google 登录和 Facebook 登录)。本教程将向您展示如何为自己的应用添加电子邮件地址和密码登录功能,以开始使用 Firebase Authentication。
将您的应用关联至 Firebase
- 安装 Firebase SDK。
- 在 Firebase 控制台中,将您的应用添加到 Firebase 项目中。
将 Firebase Authentication 添加到您的应用
将 Firebase Authentication 的依赖项添加到项目的
Podfile
中:pod 'Firebase/Auth'
运行
pod install
并打开创建的.xcworkspace
文件。
(可选)使用 Firebase Local Emulator Suite 进行原型设计和测试
在介绍应用如何对用户进行身份验证之前,我们先介绍一套可用于对身份验证功能进行原型设计和测试的工具:Firebase Local Emulator Suite。如果您正在试图确定身份验证方法和提供方、对使用 Authentication 和 Firebase 安全规则的公共和私有数据尝试不同的数据模型,或者想要对登录界面进行原型设计,那么在本地运行服务而无需实际部署服务是一种非常好的方法。
Authentication 模拟器是 Local Emulator Suite 的一部分,可让您的应用与模拟的数据库内容和配置进行交互,以及视需要与您的模拟项目资源(函数、其他数据库和安全规则)进行交互。请注意,Local Emulator Suite 尚不支持模拟的 Cloud Storage 存储。
使用 Authentication 模拟器只需完成几个步骤:
- 向应用的测试配置添加一行代码以连接到模拟器。
- 从本地项目目录的根目录运行
firebase emulators:start
。 - 使用 Local Emulator Suite 界面进行交互式原型设计,或使用 Authentication 模拟器 REST API 进行非交互式测试。
如需查看详细指南,请参阅将您的应用连接到 Authentication 模拟器。如需了解详情,请参阅 Local Emulator Suite 简介。
接下来,我们来看看如何对用户进行身份验证。
初始化 Firebase SDK
在您的应用委托中,首先导入 Firebase SDK:
Swift
import Firebase
Objective-C
@import Firebase;
然后,在 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;
// ...
}
后续步骤
了解如何添加对其他身份提供方服务和匿名访客帐号的支持: