Bạn có thể tích hợp tính năng Xác thực Firebase với hệ thống xác thực tuỳ chỉnh bằng cách
sửa đổi máy chủ xác thực của mình để tạo mã thông báo tuỳ chỉnh đã ký khi người dùng
đăng nhập thành công. Ứng dụng của bạn sẽ nhận được mã thông báo này và dùng mã đó để xác thực
với Firebase.
Trước khi bắt đầu
Làm theo các bước trong hướng dẫn Bắt đầu nếu bạn chưa làm việc này.
Khi người dùng đăng nhập vào ứng dụng của bạn, hãy gửi thông tin đăng nhập của họ (cho
tên người dùng và mật khẩu) vào máy chủ xác thực của bạn. Thông tin
máy chủ sẽ kiểm tra thông tin xác thực và nếu thông tin đó hợp lệ,
tạo mã thông báo Firebase tuỳ chỉnh
rồi gửi lại mã thông báo đó cho ứng dụng của bạn.
Sau khi bạn nhận được mã thông báo tuỳ chỉnh từ máy chủ xác thực, hãy truyền mã đó
vào signInWithCustomToken() để đăng nhập người dùng:
try{finaluserCredential=awaitFirebaseAuth.instance.signInWithCustomToken(token);print("Sign-in successful.");}onFirebaseAuthExceptioncatch(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.");}}
Các bước tiếp theo
Sau khi người dùng tạo một tài khoản mới, tài khoản này sẽ được lưu trữ như một phần của
dự án Firebase và có thể được dùng để xác định người dùng trên mọi ứng dụng trong
dự án, bất kể người dùng đã sử dụng phương thức đăng nhập nào.
Trong ứng dụng của mình, bạn có thể lấy thông tin hồ sơ cơ bản của người dùng từ
Đối tượng User. Xem phần Quản lý người dùng.
Trong Cơ sở dữ liệu theo thời gian thực của Firebase và Quy tắc bảo mật của Cloud Storage, bạn có thể
lấy mã nhận dạng người dùng duy nhất của người dùng đã đăng nhập từ biến auth rồi sử dụng mã đó để
kiểm soát loại dữ liệu mà người dùng có thể truy cập.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-25 UTC."],[],[],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();"]]