您可以使用 Firebase 身份验证允许用户使用一种或多种登录方法登录您的应用程序,包括电子邮件地址和密码登录,以及联合身份提供商,例如 Google 登录和 Facebook 登录。本教程通过向您展示如何向您的应用程序添加电子邮件地址和密码登录,帮助您开始使用 Firebase 身份验证。
将您的应用连接到 Firebase
- 安装 Firebase SDK 。
- 在Firebase 控制台中,将您的应用程序添加到您的 Firebase 项目。
将 Firebase 身份验证添加到您的应用
使用 Swift Package Manager 安装和管理 Firebase 依赖项。
- 在 Xcode 中,打开您的应用程序项目,导航至File > Add Packages 。
- 出现提示时,添加 Firebase Apple 平台 SDK 存储库:
- 选择 Firebase 身份验证库。
- 完成后,Xcode 将自动开始在后台解析和下载您的依赖项。
https://github.com/firebase/firebase-ios-sdk
(可选)使用 Firebase Local Emulator Suite 制作原型并进行测试
在讨论您的应用程序如何对用户进行身份验证之前,让我们先介绍一组可用于制作原型和测试身份验证功能的工具:Firebase Local Emulator Suite。如果您要在身份验证技术和提供者之间做出决定,使用身份验证和 Firebase 安全规则尝试使用公共和私有数据的不同数据模型,或者对登录 UI 设计进行原型设计,那么能够在不部署实时服务的情况下在本地工作可能是个好主意.
身份验证模拟器是本地模拟器套件的一部分,它使您的应用程序能够与模拟的数据库内容和配置以及可选的模拟项目资源(函数、其他数据库和安全规则)进行交互。
使用 Authentication 模拟器只需要几个步骤:
- 在您的应用程序的测试配置中添加一行代码以连接到模拟器。
- 从本地项目目录的根目录运行
firebase emulators:start
。 - 使用本地模拟器套件 UI 进行交互式原型设计,或使用身份验证模拟器 REST API 进行非交互式测试。
详细指南可在将您的应用程序连接到身份验证模拟器中找到。有关详细信息,请参阅Local Emulator Suite 介绍。
现在让我们继续介绍如何对用户进行身份验证。
初始化 Firebase SDK
在您的应用委托中,首先导入 Firebase SDK:
迅速
import FirebaseCore
目标-C
@import FirebaseCore;
然后,在application:didFinishLaunchingWithOptions:
方法中,初始化FirebaseApp
对象:
迅速
// Use Firebase library to configure APIs
FirebaseApp.configure()
目标-C
// Use Firebase library to configure APIs
[FIRApp configure];
侦听身份验证状态
对于需要有关已登录用户的信息的每个应用视图,将侦听器附加到FIRAuth
对象。只要用户的登录状态发生变化,就会调用此侦听器。
在视图控制器的viewWillAppear
方法中附加监听器:
迅速
handle = Auth.auth().addStateDidChangeListener { auth, user in
// ...
}
目标-C
self.handle = [[FIRAuth auth]
addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth, FIRUser *_Nullable user) {
// ...
}];
并在视图控制器的viewWillDisappear
方法中分离监听器:
迅速
Auth.auth().removeStateDidChangeListener(handle!)
目标-C
[[FIRAuth auth] removeAuthStateDidChangeListener:_handle];
注册新用户
创建一个表单,允许新用户使用他们的电子邮件地址和密码在您的应用程序中注册。当用户完成表单时,验证用户提供的电子邮件地址和密码,然后将它们传递给createUser
方法:
迅速
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
// ...
}
目标-C
[[FIRAuth auth] createUserWithEmail:email
password:password
completion:^(FIRAuthDataResult * _Nullable authResult,
NSError * _Nullable error) {
// ...
}];
登录现有用户
创建一个允许现有用户使用他们的电子邮件地址和密码登录的表单。当用户完成表单时,调用signIn
方法:
迅速
Auth.auth().signIn(withEmail: email, password: password) { [weak self] authResult, error in
guard let strongSelf = self else { return }
// ...
}
目标-C
[[FIRAuth auth] signInWithEmail:self->_emailField.text
password:self->_passwordField.text
completion:^(FIRAuthDataResult * _Nullable authResult,
NSError * _Nullable error) {
// ...
}];
获取用户信息
用户登录成功后,您可以获取该用户的相关信息。例如,在您的身份验证状态侦听器中:
迅速
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 += " "
}
// ...
}
目标-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;
// ...
}
下一步
了解如何添加对其他身份提供者和匿名访客帐户的支持: