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ể sử dụng tính năng Xác thực Firebase để tạo và sử dụng tài khoản ẩn danh tạm thời
để xác thực với Firebase. Những tài khoản ẩn danh tạm thời này có thể được dùng để
cho phép người dùng chưa đăng ký ứng dụng của bạn làm việc với dữ liệu được bảo vệ
theo các quy tắc bảo mật. Nếu một người dùng ẩn danh quyết định đăng ký ứng dụng của bạn, bạn có thể
liên kết thông tin đăng nhập của họ với tài khoản ẩn danh để
để họ có thể tiếp tục làm việc với dữ liệu được bảo vệ trong các phiên sau này.
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.
Bật chế độ Đăng nhập ẩn danh:
Trong phần Xác thực của bảng điều khiển của Firebase, hãy mở
Phương thức đăng nhập
.
Từ trang Phương thức đăng nhập, hãy bật tính năng Đăng nhập ẩn danh
rồi nhấp vào Lưu.
Xác thực bằng Firebase dưới dạng ẩn danh
Khi người dùng đã đăng xuất sử dụng một tính năng của ứng dụng cần phải xác thực bằng
Firebase, hãy đăng nhập người dùng ở chế độ ẩn danh bằng cách gọi signInAnonymously():
try{finaluserCredential=awaitFirebaseAuth.instance.signInAnonymously();print("Signed in with temporary account.");}onFirebaseAuthExceptioncatch(e){switch(e.code){case"operation-not-allowed":print("Anonymous auth hasn't been enabled for this project.");break;default:print("Unknown error.");}}
Chuyển đổi tài khoản ẩn danh thành tài khoản vĩnh viễn
Khi người dùng ẩn danh đăng ký ứng dụng của bạn, bạn có thể cho phép họ
tiếp tục công việc của họ với tài khoản mới—ví dụ: bạn có thể muốn
tạo các mặt hàng mà người dùng đã thêm vào giỏ hàng trước khi đăng ký
có sẵn trong giỏ hàng trong tài khoản mới của họ. Để làm như vậy, vui lòng hoàn thành các bước sau
các bước:
Khi người dùng đăng ký, hãy hoàn tất quy trình đăng nhập cho
nhà cung cấp dịch vụ xác thực tối đa, nhưng không bao gồm, gọi một trong
signInWith – phương thức. Ví dụ: lấy mã thông báo mã nhận dạng trên Google của người dùng,
Mã truy cập Facebook hoặc địa chỉ email và mật khẩu.
Nhận đối tượng Credential cho 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 đến linkWithCredential() của người dùng đăng nhập
phương thức:
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, tài khoản mới của người dùng có thể
truy cập vào dữ liệu Firebase của tài khoản ẩn danh đó.
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 Anonymously\n\n\u003cbr /\u003e\n\nYou can use Firebase Authentication to create and use temporary anonymous accounts\nto authenticate with Firebase. These temporary anonymous accounts can be used to\nallow users who haven't yet signed up to your app to work with data protected\nby security rules. If an anonymous user decides to sign up to your app, you can\n[link their sign-in credentials](/docs/auth/flutter/account-linking) to the anonymous account so\nthat they can continue to work with their protected data in future sessions.\n\nBefore you begin\n----------------\n\n1. If you haven't already, follow the steps in the [Get started](/docs/auth/flutter/start) guide.\n\n2. Enable Anonymous sign-in:\n\n - In the Firebase console's **Authentication** section, open the [Sign in method](https://console.firebase.google.com/project/_/authentication/providers) page.\n - From the **Sign in method** page, enable the **Anonymous sign-in** method and click **Save**.\n\nAuthenticate with Firebase anonymously\n--------------------------------------\n\nWhen a signed-out user uses an app feature that requires authentication with\nFirebase, sign in the user anonymously by calling `signInAnonymously()`: \n\n try {\n final userCredential =\n await FirebaseAuth.instance.signInAnonymously();\n print(\"Signed in with temporary account.\");\n } on FirebaseAuthException catch (e) {\n switch (e.code) {\n case \"operation-not-allowed\":\n print(\"Anonymous auth hasn't been enabled for this project.\");\n break;\n default:\n print(\"Unknown error.\");\n }\n }\n\n| **Note:** To protect your project from abuse, Firebase limits the number of new email/password and anonymous sign-ups that your application can have from the same IP address in a short period of time. You can request and schedule temporary changes to this quota from the [Firebase console](https://console.firebase.google.com/project/_/authentication/providers).\n\nConvert an anonymous account to a permanent account\n---------------------------------------------------\n\nWhen an anonymous user signs up to your app, you might want to allow them to\ncontinue their work with their new account---for example, you might want to\nmake the items the user added to their shopping cart before they signed up\navailable in their new account's shopping cart. To do so, complete the following\nsteps:\n\n1. When the user signs up, complete the sign-in flow for the user's\n authentication provider up to, but not including, calling one of the\n `signInWith`- methods. For example, get the user's Google ID token,\n Facebook access token, or email address and password.\n\n2. 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\n3. 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's new account can\naccess the anonymous account's Firebase data.\n| **Note:** This technique can also be used to [link any two accounts](/docs/auth/flutter/account-linking).\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();"]]