您可以使用 Firebase 身份驗證讓您的用戶使用他們的電子郵件地址和密碼通過 Firebase 進行身份驗證,並管理您應用的基於密碼的帳戶。
在你開始之前
使用 Swift Package Manager 安裝和管理 Firebase 依賴項。
- 在 Xcode 中,打開您的應用項目,導航到File > Add Packages 。
- 出現提示時,添加 Firebase Apple 平台 SDK 存儲庫:
- 選擇 Firebase 身份驗證庫。
- 完成後,Xcode 將在後台自動開始解析和下載您的依賴項。
https://github.com/firebase/firebase-ios-sdk
接下來,執行一些配置步驟:
- 如果您尚未將應用連接到 Firebase 項目,請從Firebase 控制台執行此操作。
- 啟用電子郵件/密碼登錄:
- 在Firebase 控制台中,打開Auth部分。
- 在登錄方法選項卡上,啟用電子郵件/密碼登錄方法,然後單擊保存。
創建基於密碼的帳戶
要使用密碼創建新用戶帳戶,請在應用的登錄活動中完成以下步驟:
- 在您的
UIApplicationDelegate
中導入FirebaseCore
模塊,以及您的應用委託使用的任何其他Firebase 模塊。例如,要使用 Cloud Firestore 和身份驗證:迅速
import FirebaseCore import FirebaseFirestore import FirebaseAuth // ...
Objective-C
@import FirebaseCore; @import FirebaseFirestore; @import FirebaseAuth; // ...
- 配置
FirebaseApp
共享實例,通常在App
的初始化程序或應用委託的application(_:didFinishLaunchingWithOptions:)
方法中:迅速
// Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
- 當新用戶使用您的應用程序的註冊表單進行註冊時,請完成您的應用程序所需的任何新帳戶驗證步驟,例如驗證新帳戶的密碼是否正確輸入並滿足您的複雜性要求。
- 通過將新用戶的電子郵件地址和密碼傳遞給
createUserWithEmail:email:password:completion:
來創建一個新帳戶。如果新帳戶創建成功,則用戶已登錄,您可以從傳遞給回調方法的結果對像中獲取用戶的帳戶數據。迅速
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) { // ... }];
使用電子郵件地址和密碼登錄用戶
使用密碼登錄用戶的步驟與創建新帳戶的步驟類似。在您應用的登錄活動中,執行以下操作:
- 在您的
UIApplicationDelegate
中導入FirebaseCore
模塊,以及您的應用委託使用的任何其他Firebase 模塊。例如,要使用 Cloud Firestore 和身份驗證:迅速
import FirebaseCore import FirebaseFirestore import FirebaseAuth // ...
Objective-C
@import FirebaseCore; @import FirebaseFirestore; @import FirebaseAuth; // ...
- 配置
FirebaseApp
共享實例,通常在App
的初始化程序或應用委託的application(_:didFinishLaunchingWithOptions:)
方法中:迅速
// Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
- 當用戶登錄您的應用時,將用戶的電子郵件地址和密碼傳遞給
signInWithEmail:email:password:completion:
。如果用戶成功登錄,您可以從傳遞給回調方法的結果對像中獲取用戶的帳戶數據。迅速
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) { // ... }];
下一步
用戶首次登錄後,會創建一個新用戶帳戶並將其鏈接到憑據(即用戶名和密碼、電話號碼或身份驗證提供商信息),即用戶登錄時使用的憑據。這個新帳戶作為 Firebase 項目的一部分存儲,可用於在項目中的每個應用中識別用戶,無論用戶如何登錄。
在您的 Firebase 實時數據庫和雲存儲安全規則中,您可以從
auth
變量中獲取登錄用戶的唯一用戶 ID,並使用它來控制用戶可以訪問哪些數據。
您可以通過將身份驗證提供程序憑據鏈接到現有用戶帳戶來允許用戶使用多個身份驗證提供程序登錄您的應用程序。
要註銷用戶,請調用signOut:
。
迅速
let firebaseAuth = Auth.auth() do { try firebaseAuth.signOut() } catch let signOutError as NSError { print("Error signing out: %@", signOutError) }
Objective-C
NSError *signOutError; BOOL status = [[FIRAuth auth] signOut:&signOutError]; if (!status) { NSLog(@"Error signing out: %@", signOutError); return; }
您可能還想為所有身份驗證錯誤添加錯誤處理代碼。請參閱處理錯誤。