使用自定义身份验证系统进行 Firebase 身份验证
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以通过修改身份验证服务器,在用户成功登录时生成自定义的签名令牌,将 Firebase Authentication 与自定义身份验证系统集成。您的应用会接收此令牌并将其用于 Firebase 身份验证。
准备工作
- 按照使用入门指南中的步骤操作(如果您尚未这样做)。
- 安装和配置 Firebase Admin SDK。请务必使用 Firebase 项目的正确凭据初始化 SDK。
进行 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 项目的一部分,无论用户使用哪种登录方法,您都可以使用此账号在您项目中的每个应用中识别该用户。
在您的应用中,您可以从 User
对象获取用户的基本个人资料信息。请参阅管理用户。
在您的 Firebase Realtime Database 和 Cloud Storage 安全规则中,您可以从 auth
变量获取已登录用户的唯一用户 ID,然后用其控制用户可以访问哪些数据。
您可以将身份验证服务提供方凭据关联至现有用户账号,让用户可以使用多个身份验证服务提供方登录您的应用。
如需让用户退出登录,请调用 signOut()
:
await FirebaseAuth.instance.signOut();
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-04。
[null,null,["最后更新时间 (UTC):2025-08-04。"],[],[],null,["\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\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\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\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();"]]