您可以使用Firebase身份驗證來允許用戶使用一種或多種登錄方法(包括電子郵件地址和密碼登錄)以及聯合身份提供程序(例如Google登錄和Facebook登錄)登錄到您的應用。本教程通過向您展示如何向應用程序添加電子郵件地址和密碼登錄,使您開始使用Firebase身份驗證。
將您的應用程序連接到Firebase
- 安裝Firebase SDK 。
- 在Firebase控制台中,將您的應用添加到Firebase項目中。
將Firebase身份驗證添加到您的應用
將Firebase身份驗證的依賴項添加到項目的
Podfile
:pod 'Firebase/Auth'
運行
pod install
並打開創建的.xcworkspace
文件。
(可選)使用Firebase Local Emulator Suite進行原型製作和測試
在討論您的應用程序如何對用戶進行身份驗證之前,讓我們介紹一套可用於原型化和測試身份驗證功能的工具:Firebase Local Emulator Suite。如果您要決定使用哪種身份驗證技術和提供程序,請使用“身份驗證和Firebase安全規則”或使用登錄UI設計原型來嘗試使用公共數據和私有數據創建不同的數據模型,那麼無需部署實時服務就可以在本地工作。
身份驗證模擬器是本地模擬器套件的一部分,它使您的應用程序可以與模擬的數據庫內容和配置以及可選的模擬的項目資源(功能,其他數據庫和安全規則)進行交互。請注意,本地仿真器套件尚不支持仿真的雲存儲。
使用身份驗證仿真器僅涉及幾個步驟:
- 在應用程序的測試配置中添加一行代碼以連接到仿真器。
- 在本地項目目錄的根目錄中,運行
firebase emulators:start
。 - 使用Local Emulator Suite UI進行交互式原型設計,或使用Authentication emulator REST API進行非交互式測試。
有關將您的應用程序連接到身份驗證仿真器的詳細指南,請參見。有關更多信息,請參見Local Emulator Suite簡介。
現在讓我們繼續如何驗證用戶身份。
初始化Firebase SDK
在您的應用程序委託中,首先導入Firebase SDK:
迅速
import Firebase
物鏡
@import Firebase;
然後,在application:didFinishLaunchingWithOptions:
方法中,初始化FirebaseApp
對象:
迅速
// Use Firebase library to configure APIs
FirebaseApp.configure()
物鏡
// Use Firebase library to configure APIs
[FIRApp configure];
偵聽身份驗證狀態
對於需要有關登錄用戶信息的每個應用程序視圖,請將偵聽器附加到FIRAuth
對象。每當用戶的登錄狀態更改時,都會調用此偵聽器。
將偵聽器附加到視圖控制器的viewWillAppear
方法中:
迅速
handle = Auth.auth().addStateDidChangeListener { (auth, user) in
// ...
}
物鏡
self.handle = [[FIRAuth auth]
addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth, FIRUser *_Nullable user) {
// ...
}];
並在視圖控制器的viewWillDisappear
方法中分離偵聽器:
迅速
Auth.auth().removeStateDidChangeListener(handle!)
物鏡
[[FIRAuth auth] removeAuthStateDidChangeListener:_handle];
註冊新用戶
創建一個允許新用戶使用其電子郵件地址和密碼向您的應用程序註冊的表單。用戶填寫表單後,請驗證用戶提供的電子郵件地址和密碼,然後將它們傳遞給createUser
方法:
迅速
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
// ...
}
物鏡
[[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 }
// ...
}
物鏡
[[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 += " "
}
// ...
}
物鏡
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;
// ...
}
下一步
了解如何添加對其他身份提供者和匿名來賓帳戶的支持: