您可以使用Firebase身份驗證創建並使用臨時匿名帳戶向Firebase進行身份驗證。這些臨時的匿名帳戶可用於允許尚未註冊您的應用的用戶使用受安全規則保護的數據。如果匿名用戶決定註冊您的應用,則可以將其登錄憑據鏈接到匿名帳戶,以便他們可以在以後的會話中繼續使用其受保護的數據。
在你開始之前
- 如果尚未將Firebase添加到您的Android項目中。
- 使用Firebase Android BoM ,在模塊(應用程序級)Gradle文件(通常為
app/build.gradle
)中聲明Firebase Authentication Android庫的依賴app/build.gradle
。爪哇
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:27.0.0') // Declare the dependency for the Firebase Authentication library // When using the BoM, you don't specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-auth' }
通過使用Firebase Android BoM ,您的應用將始終使用Firebase Android庫的兼容版本。
(可選)不使用BoM聲明Firebase庫依賴關係
如果選擇不使用Firebase BoM,則必須在其依賴關係行中指定每個Firebase庫版本。
請注意,如果您在應用中使用多個Firebase庫,我們強烈建議您使用BoM來管理庫版本,以確保所有版本都兼容。
dependencies { // Declare the dependency for the Firebase Authentication library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-auth:20.0.4' }
Kotlin + KTX
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:27.0.0') // Declare the dependency for the Firebase Authentication library // When using the BoM, you don't specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-auth-ktx' }
通過使用Firebase Android BoM ,您的應用將始終使用Firebase Android庫的兼容版本。
(可選)不使用BoM聲明Firebase庫依賴關係
如果選擇不使用Firebase BoM,則必須在其依賴關係行中指定每個Firebase庫版本。
請注意,如果您在應用中使用多個Firebase庫,我們強烈建議您使用BoM來管理庫版本,以確保所有版本兼容。
dependencies { // Declare the dependency for the Firebase Authentication library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-auth-ktx:20.0.4' }
- 如果您尚未將應用程序連接到Firebase項目,請從Firebase控制台進行。
- 啟用匿名身份驗證:
- 在Firebase控制台中,打開“身份驗證”部分。
- 在“登錄方法”頁面上,啟用“匿名”登錄方法。
匿名通過Firebase進行身份驗證
當註銷用戶使用需要通過Firebase進行身份驗證的應用程序功能時,請完成以下步驟以匿名方式登錄該用戶:
- 在您活動的
onCreate
方法中,獲取FirebaseAuth
對象的共享實例:爪哇
private FirebaseAuth mAuth; // ... // Initialize Firebase Auth mAuth = FirebaseAuth.getInstance();
Kotlin + KTX
private lateinit var auth: FirebaseAuth // ... // Initialize Firebase Auth auth = Firebase.auth
- 初始化活動時,請檢查用戶當前是否登錄:
爪哇
@Override public void onStart() { super.onStart(); // Check if user is signed in (non-null) and update UI accordingly. FirebaseUser currentUser = mAuth.getCurrentUser(); updateUI(currentUser); }
Kotlin + KTX
public override fun onStart() { super.onStart() // Check if user is signed in (non-null) and update UI accordingly. val currentUser = auth.currentUser updateUI(currentUser) }
- 最後,調用
signInAnonymously
以匿名用戶身份登錄:如果登錄成功,則可以使用爪哇
mAuth.signInAnonymously() .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInAnonymously:success"); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInAnonymously:failure", task.getException()); Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); updateUI(null); } } });
Kotlin + KTX
auth.signInAnonymously() .addOnCompleteListener(this) { task -> if (task.isSuccessful) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInAnonymously:success") val user = auth.currentUser updateUI(user) } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInAnonymously:failure", task.exception) Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT).show() updateUI(null) } }
getCurrentUser
方法獲取用戶的帳戶數據。
將匿名帳戶轉換為永久帳戶
當匿名用戶註冊您的應用程序時,您可能希望允許他們繼續使用其新帳戶進行工作-例如,您可能希望使用戶添加到購物車中的項目在他們註冊後可以在新帳戶中使用帳戶的購物車。為此,請完成以下步驟:
- 用戶註冊後,請完成但不包括調用
FirebaseAuth.signInWith
方法之一的用戶身份驗證提供程序的登錄流程。例如,獲取用戶的Google ID令牌,Facebook訪問令牌或電子郵件地址和密碼。 獲取新身份驗證提供程序的
AuthCredential
:Google登錄
爪哇
AuthCredential credential = GoogleAuthProvider.getCredential(googleIdToken, null);
Kotlin + KTX
val credential = GoogleAuthProvider.getCredential(googleIdToken, null)
Facebook登入
爪哇
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
Kotlin + KTX
val credential = FacebookAuthProvider.getCredential(token.token)
電子郵件密碼登錄
爪哇
AuthCredential credential = EmailAuthProvider.getCredential(email, password);
Kotlin + KTX
val credential = EmailAuthProvider.getCredential(email, password)
將
AuthCredential
對AuthCredential
傳遞給登錄用戶的linkWithCredential
方法:爪哇
mAuth.getCurrentUser().linkWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { Log.d(TAG, "linkWithCredential:success"); FirebaseUser user = task.getResult().getUser(); updateUI(user); } else { Log.w(TAG, "linkWithCredential:failure", task.getException()); Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); updateUI(null); } } });
Kotlin + KTX
auth.currentUser!!.linkWithCredential(credential) .addOnCompleteListener(this) { task -> if (task.isSuccessful) { Log.d(TAG, "linkWithCredential:success") val user = task.result?.user updateUI(user) } else { Log.w(TAG, "linkWithCredential:failure", task.exception) Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT).show() updateUI(null) } }
如果對
linkWithCredential
的調用成功,則用戶的新帳戶可以訪問匿名帳戶的Firebase數據。下一步
現在,用戶可以通過Firebase進行身份驗證,您可以使用Firebase規則來控制他們對Firebase數據庫中數據的訪問。
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2021-03-12 UTC.