您可以使用 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;
// ...
}
下一步
了解如何添加對其他身份提供者和匿名訪客帳戶的支持: