Liên kết nhiều nhà cung cấp dịch vụ xác thực với một tài khoản
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bạn có thể cho phép người dùng đăng nhập vào ứng dụng của mình bằng nhiều trình cung cấp dịch vụ xác thực bằng cách liên kết thông tin xác thực của trình cung cấp dịch vụ xác thực với một tài khoản người dùng hiện có.
Người dùng có thể nhận dạng bằng cùng một mã nhận dạng người dùng Firebase, bất kể nhà cung cấp dịch vụ xác thực mà họ dùng để đăng nhập. Ví dụ: người dùng đã đăng nhập bằng mật khẩu có thể liên kết một Tài khoản Google và đăng nhập bằng một trong hai phương thức này trong tương lai. Hoặc người dùng ẩn danh có thể liên kết tài khoản Facebook rồi sau đó đăng nhập bằng Facebook để tiếp tục sử dụng ứng dụng của bạn.
Trước khi bắt đầu
Thêm tính năng hỗ trợ cho 2 hoặc nhiều nhà cung cấp dịch vụ xác thực (có thể bao gồm cả xác thực ẩn danh) vào ứng dụng của bạn.
Liên kết thông tin đăng nhập của nhà cung cấp dịch vụ xác thực với tài khoản người dùng
Cách liên kết thông tin đăng nhập của nhà cung cấp dịch vụ xác thực với một tài khoản người dùng hiện có:
Đăng nhập cho người dùng bằng bất kỳ trình cung cấp hoặc phương thức xác thực nào.
Hoàn tất quy trình đăng nhập cho trình cung cấp dịch vụ xác thực mới, nhưng không bao gồm việc gọi một trong các phương thức signInWith. Ví dụ: lấy mã thông báo nhận dạng trên Google, mã thông báo truy cập Facebook hoặc email và mật khẩu của người dùng.
Nhận một đối tượng Credential cho trình cung cấp dịch vụ xác thực mới:
// Google Sign-infinalcredential=GoogleAuthProvider.credential(idToken:idToken);// Email and password sign-infinalcredential=EmailAuthProvider.credential(email:emailAddress,password:password);// Etc.
Truyền đối tượng Credential vào phương thức linkWithCredential() của người dùng đăng nhập:
try{finaluserCredential=awaitFirebaseAuth.instance.currentUser?.linkWithCredential(credential);}onFirebaseAuthExceptioncatch(e){switch(e.code){case"provider-already-linked":print("The provider has already been linked to the user.");break;case"invalid-credential":print("The provider's credential is not valid.");break;case"credential-already-in-use":print("The account corresponding to the credential already exists, ""or is already linked to a Firebase User.");break;// See the API reference for the full list of error codes.default:print("Unknown error.");}```
Nếu lệnh gọi đến linkWithCredential() thành công, thì giờ đây, người dùng có thể đăng nhập bằng bất kỳ trình cung cấp dịch vụ xác thực được liên kết nào và truy cập vào cùng một dữ liệu Firebase.
Huỷ liên kết nhà cung cấp dịch vụ uỷ quyền khỏi tài khoản người dùng
Bạn có thể huỷ liên kết một nhà cung cấp dịch vụ uỷ quyền khỏi một tài khoản để người dùng không thể đăng nhập bằng nhà cung cấp đó nữa.
Để huỷ liên kết một nhà cung cấp dịch vụ uỷ quyền khỏi tài khoản người dùng, hãy truyền mã nhận dạng nhà cung cấp đến phương thức unlink(). Bạn có thể lấy mã nhận dạng nhà cung cấp của các nhà cung cấp dịch vụ uỷ quyền được liên kết với một người dùng từ thuộc tính providerData của đối tượng User.
try{awaitFirebaseAuth.instance.currentUser?.unlink(providerId);}onFirebaseAuthExceptioncatch(e){switch(e.code){case"no-such-provider":print("The user isn't linked to the provider or the provider ""doesn't exist.");break;default:print("Unknown error.");}}
[[["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-08-28 UTC."],[],[],null,["\u003cbr /\u003e\n\nYou can allow users to sign in to your app using multiple authentication\nproviders by linking auth provider credentials to an existing user account.\nUsers are identifiable by the same Firebase user ID regardless of the\nauthentication provider they used to sign in. For example, a user who signed in\nwith a password can link a Google account and sign in with either method in the\nfuture. Or, an anonymous user can link a Facebook account and then, later, sign\nin with Facebook to continue using your app.\n\nBefore you begin\n\nAdd support for two or more authentication providers (possibly including\nanonymous authentication) to your app.\n\nLink auth provider credentials to a user account\n\nTo link auth provider credentials to an existing user account:\n\n1. Sign in the user using any authentication provider or method.\n\n2. Complete the sign-in flow for the new authentication provider up to, but not\n including, calling one of the `signInWith`- methods. For example, get\n the user's Google ID token, Facebook access token, or email and password.\n\n3. Get a `Credential` object for the new authentication provider:\n\n // Google Sign-in\n final credential = GoogleAuthProvider.credential(idToken: idToken);\n\n // Email and password sign-in\n final credential =\n EmailAuthProvider.credential(email: emailAddress, password: password);\n\n // Etc.\n\n4. Pass the `Credential` object to the sign-in user's `linkWithCredential()`\n method:\n\n try {\n final userCredential = await FirebaseAuth.instance.currentUser\n ?.linkWithCredential(credential);\n } on FirebaseAuthException catch (e) {\n switch (e.code) {\n case \"provider-already-linked\":\n print(\"The provider has already been linked to the user.\");\n break;\n case \"invalid-credential\":\n print(\"The provider's credential is not valid.\");\n break;\n case \"credential-already-in-use\":\n print(\"The account corresponding to the credential already exists, \"\n \"or is already linked to a Firebase User.\");\n break;\n // See the API reference for the full list of error codes.\n default:\n print(\"Unknown error.\");\n }\n ```\n\nIf the call to `linkWithCredential()` succeeds, the user can now sign in using\nany linked authentication provider and access the same Firebase data.\n\nUnlink an auth provider from a user account\n\nYou can unlink an auth provider from an account, so that the user can no\nlonger sign in with that provider.\n\nTo unlink an auth provider from a user account, pass the provider ID to the\n`unlink()` method. You can get the provider IDs of the auth providers linked to\na user from the `User` object's `providerData` property. \n\n try {\n await FirebaseAuth.instance.currentUser?.unlink(providerId);\n } on FirebaseAuthException catch (e) {\n switch (e.code) {\n case \"no-such-provider\":\n print(\"The user isn't linked to the provider or the provider \"\n \"doesn't exist.\");\n break;\n default:\n print(\"Unknown error.\");\n }\n }\n\nTroubleshooting\n\nIf you encounter errors when trying to link multiple accounts, see the\n[documentation on verified email addresses](https://firebase.google.com/docs/auth/users#verified_email_addresses)."]]