使用自訂驗證系統向 Firebase 進行驗證
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可透過以下方式整合 Firebase 驗證與自訂驗證系統:
修改您的驗證伺服器,以便在使用者
才能成功登入應用程式接收這個權杖,並使用該權杖進行驗證
掌握實用知識
事前準備
- 如果您尚未安裝,請按照入門指南中的步驟操作。
- 安裝並設定 Firebase Admin SDK。
請務必初始化 SDK
並使用 Firebase 專案的正確憑證。
透過 Firebase 驗證
使用者登入應用程式時,請將登入憑證
例如他們的使用者名稱和密碼)。您的
伺服器會檢查憑證,如果憑證有效
建立自訂 Firebase 權杖
然後將權杖傳回應用程式
從驗證伺服器收到自訂權杖後,請將該權杖傳遞
以便 signInWithCustomToken()
登入使用者:
try {
final userCredential =
await FirebaseAuth.instance.signInWithCustomToken(token);
print("Sign-in successful.");
} on FirebaseAuthException catch (e) {
switch (e.code) {
case "invalid-custom-token":
print("The supplied token is not a Firebase custom auth token.");
break;
case "custom-token-mismatch":
print("The supplied token is for a different Firebase project.");
break;
default:
print("Unknown error.");
}
}
後續步驟
使用者建立新帳戶後,系統會將這個帳戶儲存為您的
Firebase 專案的專用 ID,可用來識別應用程式內所有應用程式的使用者
專案,無論使用者使用的登入方式為何。
在您的應用程式中,您可以透過
User
物件。請參閱管理使用者。
在 Firebase 即時資料庫和 Cloud Storage 安全性規則中,您可以
透過 auth
變數取得已登入使用者的不重複使用者 ID,並用於
控制使用者可以存取哪些資料
您可以讓使用者透過多重驗證機制登入您的應用程式
將驗證供應商憑證連結)
現有的使用者帳戶。
如要將使用者登出,請呼叫 signOut()
:
await FirebaseAuth.instance.signOut();
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[],[],null,["# Authenticate with Firebase Using a Custom Authentication System\n\n\u003cbr /\u003e\n\nYou can integrate Firebase Authentication with a custom authentication system by\nmodifying your authentication server to produce custom signed tokens when a user\nsuccessfully signs in. Your app receives this token and uses it to authenticate\nwith Firebase.\n\nBefore you begin\n----------------\n\n1. If you haven't already, follow the steps in the [Get started](/docs/auth/flutter/start) guide.\n2. [Install and configure the Firebase Admin SDK](/docs/admin/setup). Be sure to [initialize the SDK](/docs/admin/setup#initialize-sdk) with the correct credentials for your Firebase project.\n\nAuthenticate with Firebase\n--------------------------\n\n1. When users sign in to your app, send their sign-in credentials (for\n example, their username and password) to your authentication server. Your\n server checks the credentials and, if they are valid,\n [creates a custom Firebase token](/docs/auth/admin/create-custom-tokens)\n and sends the token back to your app.\n\n2. After you receive the custom token from your authentication server, pass it\n to `signInWithCustomToken()` to sign in the user:\n\n try {\n final userCredential =\n await FirebaseAuth.instance.signInWithCustomToken(token);\n print(\"Sign-in successful.\");\n } on FirebaseAuthException catch (e) {\n switch (e.code) {\n case \"invalid-custom-token\":\n print(\"The supplied token is not a Firebase custom auth token.\");\n break;\n case \"custom-token-mismatch\":\n print(\"The supplied token is for a different Firebase project.\");\n break;\n default:\n print(\"Unknown error.\");\n }\n }\n\nNext steps\n----------\n\nAfter a user creates a new account, this account is stored as part of your\nFirebase project, and can be used to identify a user across every app in your\nproject, regardless of what sign-in method the user used.\n\nIn your apps, you can get the user's basic profile information from the\n`User` object. See [Manage Users](/docs/auth/flutter/manage-users).\n\nIn your Firebase Realtime Database and Cloud Storage Security Rules, you can\nget the signed-in user's unique user ID from the `auth` variable, and use it to\ncontrol what data a user can access.\n\nYou can allow users to sign in to your app using multiple authentication\nproviders by [linking auth provider credentials](/docs/auth/flutter/account-linking)) to an\nexisting user account.\n\nTo sign out a user, call `signOut()`: \n\n await FirebaseAuth.instance.signOut();"]]