您可以使用 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 控制台中,打開“身份驗證”部分。
- 在登錄方法選項卡上,啟用電子郵件/密碼登錄方法,然後單擊保存。
創建基於密碼的帳戶
要創建帶有密碼的新用戶帳戶,請在應用程序的登錄活動中完成以下步驟:
- 在
UIApplicationDelegate
中導入FirebaseCore
模塊,以及應用程序委託使用的任何其他Firebase 模塊。例如,要使用 Cloud Firestore 和身份驗證:斯威夫特用戶界面
import SwiftUI import FirebaseCore import FirebaseFirestore import FirebaseAuth // ...
迅速
import FirebaseCore import FirebaseFirestore import FirebaseAuth // ...
Objective-C
@import FirebaseCore; @import FirebaseFirestore; @import FirebaseAuth; // ...
- 在應用程序委託的
application(_:didFinishLaunchingWithOptions:)
方法中配置FirebaseApp
共享實例:斯威夫特用戶界面
// Use Firebase library to configure APIs FirebaseApp.configure()
迅速
// Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
- 如果您使用 SwiftUI,則必須創建一個應用程序委託並通過
UIApplicationDelegateAdaptor
或NSApplicationDelegateAdaptor
將其附加到您的App
結構。您還必須禁用應用程序委託調配。有關更多信息,請參閱SwiftUI 說明。斯威夫特用戶界面
@main struct YourApp: App { // register app delegate for Firebase setup @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate var body: some Scene { WindowGroup { NavigationView { ContentView() } } } }
- 當新用戶使用應用程序的註冊表單進行註冊時,請完成應用程序要求的所有新帳戶驗證步驟,例如驗證新帳戶的密碼是否正確輸入並滿足您的複雜性要求。
- 通過將新用戶的電子郵件地址和密碼傳遞給
createUser
來創建新帳戶。如果新帳戶已成功創建,則用戶已登錄,您可以從傳遞給回調方法的結果對像中獲取用戶的帳戶數據。迅速
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 SwiftUI import FirebaseCore import FirebaseFirestore import FirebaseAuth // ...
迅速
import FirebaseCore import FirebaseFirestore import FirebaseAuth // ...
Objective-C
@import FirebaseCore; @import FirebaseFirestore; @import FirebaseAuth; // ...
- 在應用程序委託的
application(_:didFinishLaunchingWithOptions:)
方法中配置FirebaseApp
共享實例:斯威夫特用戶界面
// Use Firebase library to configure APIs FirebaseApp.configure()
迅速
// Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
- 如果您使用 SwiftUI,則必須創建一個應用程序委託並通過
UIApplicationDelegateAdaptor
或NSApplicationDelegateAdaptor
將其附加到您的App
結構。您還必須禁用應用程序委託調配。有關更多信息,請參閱SwiftUI 說明。斯威夫特用戶界面
@main struct YourApp: App { // register app delegate for Firebase setup @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate var body: some Scene { WindowGroup { NavigationView { ContentView() } } } }
- 當用戶登錄您的應用程序時,將用戶的電子郵件地址和密碼傳遞給
signIn
。如果用戶成功登錄,您可以從傳遞給回調方法的結果對像中獲取用戶的帳戶數據。迅速
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) { // ... }];
建議:啟用電子郵件枚舉保護
如果電子郵件地址在必須註冊時未註冊(例如,使用電子郵件地址和密碼登錄時)或在必須未使用時已註冊(例如,更改用戶的電子郵件地址時)。雖然這有助於向用戶建議具體補救措施,但惡意行為者也可能會濫用它來發現用戶註冊的電子郵件地址。
為了降低此風險,我們建議您使用 Google Cloud gcloud
工具為您的項目啟用電子郵件枚舉保護。請注意,啟用此功能會更改 Firebase 身份驗證的錯誤報告行為:確保您的應用不依賴於更具體的錯誤。
下一步
用戶首次登錄後,系統會創建一個新的用戶帳戶,並將其鏈接到用戶登錄時使用的憑據(即用戶名和密碼、電話號碼或身份驗證提供商信息)。此新帳戶將作為 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; }
您可能還需要為所有身份驗證錯誤添加錯誤處理代碼。請參閱處理錯誤。