您可以使用 Firebase 驗證讓您的使用者使用其電子郵件地址和密碼透過 Firebase 進行身份驗證,並管理您套用的基於密碼的帳戶。
在你開始之前
使用 Swift Package Manager 安裝和管理 Firebase 相依性。
- 在 Xcode 中,開啟應用程式項目,導覽至File > Add Packages 。
- 出現提示時,新增 Firebase Apple 平台 SDK 儲存庫:
- 選擇 Firebase 身份驗證庫。
- 將
-ObjC
標誌新增至目標建置設定的「其他連結器標誌」部分。 - 完成後,Xcode 將自動開始在背景解析並下載您的依賴項。
https://github.com/firebase/firebase-ios-sdk.git
接下來,執行一些設定步驟:
- 如果您尚未將應用程式連線至 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; }
您可能還需要為所有身份驗證錯誤新增錯誤處理代碼。請參閱處理錯誤。