在 Apple 平台上使用自訂驗證系統進行 Firebase 驗證

您可以修改驗證伺服器,在使用者成功登入時產生自訂簽署權杖,藉此將 Firebase Authentication 與自訂驗證系統整合。應用程式會接收這個權杖,並用於透過 Firebase 進行驗證。

事前準備

  1. 如果您尚未建立 Firebase 專案並註冊應用程式,請先完成這些步驟。
  2. 使用 Swift Package Manager 安裝及管理 Firebase 依附元件。

    1. 在 Xcode 中保持開啟應用程式專案,然後依序點選「File」>「Add Packages」
    2. 系統提示時,請新增 Firebase Apple 平台 SDK 存放區:
    3.   https://github.com/firebase/firebase-ios-sdk.git
    4. 選擇 Firebase Authentication 程式庫。
    5. -ObjC 標記新增至目標的建構設定「Other Linker Flags」部分。
    6. 完成後,Xcode 就會自動開始在背景中解析並下載依附元件。
  3. 取得專案的伺服器金鑰:
    1. 前往專案設定中的「服務帳戶」頁面。
    2. 在「Service Accounts」(服務帳戶) 頁面的「Firebase Admin SDK」部分底部,按一下「Generate New Private Key」
    3. 系統會自動將新服務帳戶的公開/私密金鑰組儲存至您的電腦。將這個檔案複製到驗證伺服器。

使用 Firebase 進行驗證

  1. UIApplicationDelegate 中匯入 FirebaseCore 模組,以及應用程式委派程式使用的任何其他 Firebase 模組。例如,如要使用 Cloud FirestoreAuthentication
    import SwiftUI
    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          
    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          
    @import FirebaseCore;
    @import FirebaseFirestore;
    @import FirebaseAuth;
    // ...
          
  2. 在應用程式委派作業的 application(_:didFinishLaunchingWithOptions:) 方法中,設定 FirebaseApp 共用例項:
    // Use Firebase library to configure APIs
    FirebaseApp.configure()
    // Use Firebase library to configure APIs
    FirebaseApp.configure()
    // Use Firebase library to configure APIs
    [FIRApp configure];
  3. 如果您使用 SwiftUI,則必須建立應用程式委派程式,並透過 UIApplicationDelegateAdaptorNSApplicationDelegateAdaptor 將其附加至 App 結構體。您也必須停用應用程式委派程式 swizzling。詳情請參閱 SwiftUI 操作說明
    SwiftUI
    @main
    struct YourApp: App {
      // register app delegate for Firebase setup
      @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    
      var body: some Scene {
        WindowGroup {
          NavigationView {
            ContentView()
          }
        }
      }
    }
          
  4. 當使用者登入應用程式時,將其登入憑證 (例如使用者名稱和密碼) 傳送至驗證伺服器。您的伺服器會檢查憑證,並在憑證有效時傳回自訂權杖
  5. 從驗證伺服器收到自訂權杖後,請將其傳遞至 signInWithCustomToken 以登入使用者:
    SwiftObjective-C
    Auth.auth().signIn(withCustomToken: customToken ?? "") { user, error in
      // ...
    }
    [[FIRAuth auth] signInWithCustomToken:customToken
                               completion:^(FIRAuthDataResult * _Nullable authResult,
                                            NSError * _Nullable error) {
      // ...
    }];

後續步驟

使用者首次登入後,系統會建立新使用者帳戶,並連結至使用者登入時所用的憑證 (即使用者名稱和密碼、電話號碼或驗證服務提供者資訊)。這個新帳戶會儲存在 Firebase 專案中,無論使用者如何登入,都可以用於在專案中的每個應用程式中識別使用者。

  • 在應用程式中,您可以從 User 物件取得使用者的個人資料基本資訊。請參閱「管理使用者」。

  • Firebase Realtime DatabaseCloud Storage 安全性規則中,您可以從 auth 變數取得已登入使用者的專屬使用者 ID,並利用該 ID 控管使用者可存取的資料。

您可以將驗證服務供應商憑證連結至現有使用者帳戶,讓使用者使用多個驗證服務供應商登入應用程式。

如要讓使用者登出,請呼叫 signOut:

SwiftObjective-C
let firebaseAuth = Auth.auth()
do {
  try firebaseAuth.signOut()
} catch let signOutError as NSError {
  print("Error signing out: %@", signOutError)
}
NSError *signOutError;
BOOL status = [[FIRAuth auth] signOut:&signOutError];
if (!status) {
  NSLog(@"Error signing out: %@", signOutError);
  return;
}

您可能還想為各種驗證錯誤新增錯誤處理程式碼。請參閱「處理錯誤」。

Firebase Authentication lets you add an end-to-end identity solution to your app for easy user authentication, sign-in, and onboarding in just a few lines of code.

更新時間:Mar 7, 2025

Firebase Authentication lets you add an end-to-end identity solution to your app for easy user authentication, sign-in, and onboarding in just a few lines of code.

更新時間:Mar 7, 2025