您可以使用 Firebase 驗證功能,透過傳送簡訊到使用者的手機登入使用者。使用者使用簡訊中的一次性代碼登入。
如要在應用程式中加入電話號碼登入程序,最簡單的方法就是使用 FirebaseUI,其中包含一個用於實作電話號碼登入流程的置入登入小工具,以及密碼功能和聯合登入。本文說明如何使用 Firebase SDK 導入電話號碼登入流程。
事前準備
- 如果您尚未將 Firebase 新增至 Android 專案,請先完成這項操作。
-
在模組 (應用程式層級) Gradle 檔案 (通常是
<project>/<app-module>/build.gradle.kts
或<project>/<app-module>/build.gradle
) 中,新增 Android 專用 Firebase 驗證程式庫的依附元件。dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:33.1.1")) // Add 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 程式庫版本。
(替代做法) 新增 Firebase 程式庫依附元件,「不」使用 BoM
如果選擇不使用 Firebase BoM,就必須在依附元件行中指定每個 Firebase 程式庫版本。
請注意,如果您在應用程式中使用多個 Firebase 程式庫,強烈建議您使用 BoM 來管理程式庫版本,以確保所有版本都相容。
dependencies { // Add 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:23.0.0") }
- 如果您尚未將應用程式連結至 Firebase 專案,請透過 Firebase 控制台進行。
- 如果您尚未在 Firebase 控制台設定應用程式的 SHA-1 雜湊,請採取相關做法。如要瞭解如何找出應用程式的 SHA-1 雜湊,請參閱「 驗證用戶端」。
安全疑慮
僅使用電話號碼進行驗證,但比其他可用的方法更安全,因為使用者可以在使用者之間輕鬆轉移電話號碼。此外,如果裝置上有多個使用者設定檔,任何可接收簡訊的使用者,都能使用裝置的電話號碼登入帳戶。
如果您在應用程式中使用以電話號碼為基礎的登入功能,建議您除了提供更安全的登入方式之外,也應向使用者說明使用電話號碼登入機制在安全性方面的保障。
為 Firebase 專案啟用電話號碼登入功能
如要透過簡訊登入使用者,您必須先為 Firebase 專案啟用電話號碼登入方式:
- 在 Firebase 控制台開啟「驗證」專區。
- 在「Sign-in Method」(登入方式) 頁面中,啟用「Phone Number」(電話號碼) 登入方式。
Firebase 的電話號碼登入要求配額夠高,因此大多數應用程式不會受到影響。不過,如果您需要透過電話驗證來登入大量使用者,則可能需要升級定價方案。請參閱定價頁面。
啟用應用程式驗證功能
如要使用電話號碼驗證功能,Firebase 必須能夠驗證電話號碼登入要求是否來自您的應用程式。Firebase 驗證功能會透過三種方式完成這項操作:
- Play Integrity API:如果使用者的裝置已安裝 Google Play 服務,且 Firebase 驗證功能可透過 Play Integrity API 驗證裝置是否合法,使用者只需以電話號碼登入程序即可。已透過 Firebase 驗證 (而非專案) 在 Google 自有專案中啟用 Play Integrity API。這不會計入專案的任何 Play Integrity API 配額。Authentication SDK v21.2.0+ (Firebase BoM v31.4.0 以上版本) 提供 Play Integrity 支援。
如要使用 Play Integrity,如果您尚未指定應用程式的 SHA-256 指紋,請前往 Firebase 控制台的「專案設定」指定指紋。如要進一步瞭解如何取得應用程式的 SHA-256 指紋,請參閱「驗證用戶端」一文。
- reCAPTCHA 驗證:如果無法使用 Play Integrity,例如使用者裝置「未」安裝 Google Play 服務,Firebase 驗證就會使用 reCAPTCHA 驗證機製完成手機登入流程。使用者通常可以完成 reCAPTCHA 驗證問題,不需要解決任何問題。請注意,這個流程需要 SHA-1 與您的應用程式相關聯。如要採取這個流程,您也必須為
PROJECT_ID.firebaseapp.com
將 API 金鑰設為未限製或加入許可清單。以下列舉幾個觸發 reCAPTCHA 的情況:
- 如果使用者的裝置未安裝 Google Play 服務,
- 如果您的應用程式不是透過 Google Play 商店發行 (使用 Authentication SDK v21.2.0+)。
- 如果取得的 SafetyNet 權杖無效 (在 Authentication SDK 版本 < 21.2.0 中) 無效,
使用 SafetyNet 或 Play Integrity 進行應用程式驗證時,簡訊範本的
%APP_NAME%
欄位會填入從 Google Play 商店判定的應用程式名稱。在觸發 reCAPTCHA 的情況下,系統會將%APP_NAME%
填入PROJECT_ID.firebaseapp.com
。
forceRecaptchaFlowForTesting
強制執行 reCAPTCHA 驗證流程。
您可以使用 setAppVerificationDisabledForTesting
,停用應用程式驗證功能 (使用虛構電話號碼時)。
疑難排解
使用 reCAPTCHA 驗證應用程式時發生「缺少初始狀態」錯誤
當 reCAPTCHA 流程成功完成,但並未將使用者重新導向回原生應用程式,就可能會發生這種情況。在這種情況下,系統會將使用者重新導向至備用網址
PROJECT_ID.firebaseapp.com/__/auth/handler
。Firefox 瀏覽器預設會停用開啟原生應用程式的連結。如果 Firefox 顯示上述錯誤訊息,請按照「將 Firefox for Android 設為在原生應用程式中開啟連結」一文中的步驟,啟用應用程式連結。
將驗證碼傳送至使用者的手機
如要啟動電話號碼登入程序,請提供使用者介面,提示使用者輸入電話號碼。相關法律要求各有不同,但您應告知使用者,讓他們使用手機登入時,可能會收到驗證簡訊和標準費率的簡訊。
接著,將電話號碼傳送至 PhoneAuthProvider.verifyPhoneNumber
方法,要求 Firebase 驗證使用者的電話號碼。例如:
Kotlin+KTX
val options = PhoneAuthOptions.newBuilder(auth) .setPhoneNumber(phoneNumber) // Phone number to verify .setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit .setActivity(this) // Activity (for callback binding) .setCallbacks(callbacks) // OnVerificationStateChangedCallbacks .build() PhoneAuthProvider.verifyPhoneNumber(options)
Java
PhoneAuthOptions options = PhoneAuthOptions.newBuilder(mAuth) .setPhoneNumber(phoneNumber) // Phone number to verify .setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit .setActivity(this) // (optional) Activity for callback binding // If no activity is passed, reCAPTCHA verification can not be used. .setCallbacks(mCallbacks) // OnVerificationStateChangedCallbacks .build(); PhoneAuthProvider.verifyPhoneNumber(options);
verifyPhoneNumber
方法會重新輸入:如果多次呼叫 (例如在活動的 onStart
方法中),verifyPhoneNumber
方法將不會傳送第二則簡訊,除非原始要求已逾時。
如果您的應用程式在使用者能夠登入前就關閉 (例如使用者使用簡訊應用程式時),您可以使用這個行為繼續執行電話號碼登入程序。呼叫 verifyPhoneNumber
後,請設定標記,表示正在進行驗證。接著,將旗標儲存在活動的 onSaveInstanceState
方法中,並在 onRestoreInstanceState
中還原旗標。最後,在活動的 onStart
方法中,檢查驗證程序是否已經進行,如果有,請再次呼叫 verifyPhoneNumber
。請務必在驗證完成或失敗時清除標記 (請參閱「
驗證回呼」)。
如要輕鬆處理螢幕旋轉和其他活動重新啟動的情況,請將「活動」傳遞至 verifyPhoneNumber
方法。當活動停止時,系統會自動卸離回呼,因此您可以在回呼方法中自由寫入 UI 轉換程式碼。
您也可以透過驗證執行個體上的 setLanguageCode
方法指定授權語言,藉此本地化 Firebase 傳送的簡訊。
Kotlin+KTX
auth.setLanguageCode("fr") // To apply the default app language instead of explicitly setting it. // auth.useAppLanguage()
Java
auth.setLanguageCode("fr"); // To apply the default app language instead of explicitly setting it. // auth.useAppLanguage();
呼叫 PhoneAuthProvider.verifyPhoneNumber
時,您也必須提供 OnVerificationStateChangedCallbacks
的例項,其中包含處理要求結果的回呼函式實作。例如:
Kotlin+KTX
callbacks = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() { override fun onVerificationCompleted(credential: PhoneAuthCredential) { // This callback will be invoked in two situations: // 1 - Instant verification. In some cases the phone number can be instantly // verified without needing to send or enter a verification code. // 2 - Auto-retrieval. On some devices Google Play services can automatically // detect the incoming verification SMS and perform verification without // user action. Log.d(TAG, "onVerificationCompleted:$credential") signInWithPhoneAuthCredential(credential) } override fun onVerificationFailed(e: FirebaseException) { // This callback is invoked in an invalid request for verification is made, // for instance if the the phone number format is not valid. Log.w(TAG, "onVerificationFailed", e) if (e is FirebaseAuthInvalidCredentialsException) { // Invalid request } else if (e is FirebaseTooManyRequestsException) { // The SMS quota for the project has been exceeded } else if (e is FirebaseAuthMissingActivityForRecaptchaException) { // reCAPTCHA verification attempted with null Activity } // Show a message and update the UI } override fun onCodeSent( verificationId: String, token: PhoneAuthProvider.ForceResendingToken, ) { // The SMS verification code has been sent to the provided phone number, we // now need to ask the user to enter the code and then construct a credential // by combining the code with a verification ID. Log.d(TAG, "onCodeSent:$verificationId") // Save verification ID and resending token so we can use them later storedVerificationId = verificationId resendToken = token } }
Java
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(@NonNull PhoneAuthCredential credential) { // This callback will be invoked in two situations: // 1 - Instant verification. In some cases the phone number can be instantly // verified without needing to send or enter a verification code. // 2 - Auto-retrieval. On some devices Google Play services can automatically // detect the incoming verification SMS and perform verification without // user action. Log.d(TAG, "onVerificationCompleted:" + credential); signInWithPhoneAuthCredential(credential); } @Override public void onVerificationFailed(@NonNull FirebaseException e) { // This callback is invoked in an invalid request for verification is made, // for instance if the the phone number format is not valid. Log.w(TAG, "onVerificationFailed", e); if (e instanceof FirebaseAuthInvalidCredentialsException) { // Invalid request } else if (e instanceof FirebaseTooManyRequestsException) { // The SMS quota for the project has been exceeded } else if (e instanceof FirebaseAuthMissingActivityForRecaptchaException) { // reCAPTCHA verification attempted with null Activity } // Show a message and update the UI } @Override public void onCodeSent(@NonNull String verificationId, @NonNull PhoneAuthProvider.ForceResendingToken token) { // The SMS verification code has been sent to the provided phone number, we // now need to ask the user to enter the code and then construct a credential // by combining the code with a verification ID. Log.d(TAG, "onCodeSent:" + verificationId); // Save verification ID and resending token so we can use them later mVerificationId = verificationId; mResendToken = token; } };
驗證回呼
在大多數應用程式中,您會實作 onVerificationCompleted
、onVerificationFailed
和 onCodeSent
回呼。視應用程式的需求而定,您也可以實作 onCodeAutoRetrievalTimeOut
。
onVerificationCompleted(PhoneAuthCredential)
系統會在兩種情況下呼叫這個方法:
- 即時驗證:在某些情況下,不必傳送或輸入驗證碼,就能立即驗證電話號碼。
- 自動擷取:在部分裝置上,Google Play 服務可以自動偵測收到的驗證簡訊,並讓使用者無須採取任何動作即可進行驗證。(部分電信業者可能無法使用這項功能)。這會使用 SMS Retriever API,也就是簡訊結尾處包含 11 個字元的雜湊值。
PhoneAuthCredential
物件登入使用者。
onVerificationFailed(FirebaseException)
為回應無效的驗證要求,例如指定無效電話號碼或驗證碼的要求,系統會呼叫此方法。
onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken)
選用設定。透過簡訊將驗證碼傳送至所提供的電話號碼後,系統就會呼叫此方法。
呼叫此方法時,大多數應用程式會顯示 UI,提示使用者輸入簡訊中的驗證碼。同時,自動驗證作業可能會在背景繼續進行。使用者輸入驗證碼後,您就能使用驗證碼和傳遞至方法的驗證 ID 建立 PhoneAuthCredential
物件,然後據此登入使用者。不過,部分應用程式可能要等到呼叫 onCodeAutoRetrievalTimeOut
後,才會顯示驗證碼 UI (不建議)。
onCodeAutoRetrievalTimeOut(String verificationId)
選用設定。指定 verifyPhoneNumber
的逾時時間過後,系統就會呼叫此方法,且不會先觸發 onVerificationCompleted
。在沒有 SIM 卡的裝置上,由於系統不支援自動擷取簡訊,因此系統會立即呼叫這個方法。
有些應用程式會阻止使用者輸入內容,直到自動驗證期逾時,然後只顯示提示使用者輸入簡訊驗證碼的使用者介面 (不建議採用)。
建立 PhoneAuthCredential 物件
使用者輸入 Firebase 傳送到使用者手機的驗證碼後,請使用傳遞至 onCodeSent
或 onCodeAutoRetrievalTimeOut
回呼的驗證碼和驗證 ID 建立 PhoneAuthCredential
物件。(呼叫 onVerificationCompleted
時,會直接取得 PhoneAuthCredential
物件,因此可以略過這個步驟)。
如要建立 PhoneAuthCredential
物件,請呼叫 PhoneAuthProvider.getCredential
:
Kotlin+KTX
val credential = PhoneAuthProvider.getCredential(verificationId!!, code)
Java
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
登入使用者帳戶
取得 PhoneAuthCredential
物件後,無論是在 onVerificationCompleted
回呼或呼叫 PhoneAuthProvider.getCredential
中,都必須將 PhoneAuthCredential
物件傳遞至 FirebaseAuth.signInWithCredential
以完成登入流程:
Kotlin+KTX
private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential) { auth.signInWithCredential(credential) .addOnCompleteListener(this) { task -> if (task.isSuccessful) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithCredential:success") val user = task.result?.user } else { // Sign in failed, display a message and update the UI Log.w(TAG, "signInWithCredential:failure", task.exception) if (task.exception is FirebaseAuthInvalidCredentialsException) { // The verification code entered was invalid } // Update UI } } }
Java
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) { mAuth.signInWithCredential(credential) .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, "signInWithCredential:success"); FirebaseUser user = task.getResult().getUser(); // Update UI } else { // Sign in failed, display a message and update the UI Log.w(TAG, "signInWithCredential:failure", task.getException()); if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) { // The verification code entered was invalid } } } }); }
使用虛構的電話號碼進行測試
您可以透過 Firebase 控制台設定用於開發的虛構電話號碼。使用虛構的電話號碼進行測試有以下優點:
- 測試電話號碼驗證,而不耗用您的使用配額。
- 在不傳送實際簡訊的情況下,測試電話號碼的驗證方式。
- 以相同的電話號碼連續執行測試,而不會受到限制。這種做法可以盡可能降低應用程式商店審查流程中遭到拒絕的風險。這樣一來,如果審查人員當初是以同一組電話號碼進行測試,他們就會使用同一組電話號碼進行測試。
- 無須額外費心,即可在開發環境中輕鬆進行測試,例如可以在 iOS 模擬器中進行開發,或是在沒有 Google Play 服務的 Android 模擬器中進行開發。
- 編寫整合測試時,不會因實際電話號碼在正式環境中執行安全性檢查而遭到封鎖。
虛構電話號碼必須符合下列規定:
- 請務必使用虛構的電話號碼,而且尚未取得。 Firebase 驗證無法將真人使用者目前使用的電話號碼設為測試號碼。其中一種選項是使用 555 組的號碼做為美國測試電話號碼,例如:+1 650-555-3434
- 電話號碼必須符合長度和其他限制的正確格式。不過,他們仍須完成與實際使用者電話號碼相同的驗證。
- 您最多可以新增 10 組開發用電話號碼。
- 請使用難以猜到及變動的測試電話號碼/代碼。
建立虛構的電話號碼和驗證碼
- 在 Firebase 控制台開啟「驗證」專區。
- 在「登入方式」分頁中啟用電話服務供應商 (如果您尚未啟用的話)。
- 開啟「測試用電話號碼」選單。
- 提供要測試的電話號碼,例如 +1 650-555-3434。
- 提供該特定號碼的 6 位數驗證碼,例如「654321」。
- 新增數字。如果需要,您可以刪除電話號碼及其程式碼,只要將滑鼠遊標懸停在對應資料列上,再按一下垃圾桶圖示即可。
手動測試
您可以直接開始在應用程式中使用虛構的電話號碼。這樣一來,您就能在開發階段執行手動測試,而不會遇到配額問題或節流問題。您也可以直接在未安裝 Google Play 服務的 iOS 模擬器或 Android 模擬器中進行測試。
如果提供虛構電話號碼並傳送驗證碼,系統不會傳送實際簡訊。您必須改為提供先前設定的驗證碼,才能完成登入程序。
登入完成後,系統會使用該電話號碼建立 Firebase 使用者。使用者和實際電話號碼使用者的行為和屬性相同,也能以相同方式存取即時資料庫/Cloud Firestore 和其他服務。在這個過程中,建立的 ID 權杖與實際電話號碼使用者的簽章相同。
如果想要進一步限制存取權,您也可以透過自訂聲明為這些使用者設定測試角色,藉此區分他們為假使用者。
如要手動觸發 reCAPTCHA 流程以進行測試,請使用 forceRecaptchaFlowForTesting()
方法。
// Force reCAPTCHA flow FirebaseAuth.getInstance().getFirebaseAuthSettings().forceRecaptchaFlowForTesting();
整合測試
除了手動測試之外,Firebase 驗證也提供 API,協助您撰寫手機驗證測試的整合測試。這些 API 會停用網頁版 reCAPTCHA 要求,並在 iOS 中停用靜音推播通知,藉此停用應用程式驗證功能。如此一來,就能在這些流程中進行自動化測試,更輕鬆地實作。還可讓您測試 Android 上的即時驗證流程。
在 Android 上,請在 signInWithPhoneNumber
呼叫之前呼叫 setAppVerificationDisabledForTesting()
。這樣可自動停用應用程式驗證功能,讓您不需手動解決電話號碼,就能傳送電話號碼。即使 Play Integrity 和 reCAPTCHA 已停用,使用實際電話號碼仍會無法完成登入。這個 API 只能使用虛構的電話號碼。
// Turn off phone auth app verification. FirebaseAuth.getInstance().getFirebaseAuthSettings() .setAppVerificationDisabledForTesting();
使用虛構號碼呼叫 verifyPhoneNumber
會觸發 onCodeSent
回呼,您必須提供對應的驗證碼。這可讓您在 Android Emulator 中進行測試。
Java
String phoneNum = "+16505554567"; String testVerificationCode = "123456"; // Whenever verification is triggered with the whitelisted number, // provided it is not set for auto-retrieval, onCodeSent will be triggered. FirebaseAuth auth = FirebaseAuth.getInstance(); PhoneAuthOptions options = PhoneAuthOptions.newBuilder(auth) .setPhoneNumber(phoneNum) .setTimeout(60L, TimeUnit.SECONDS) .setActivity(this) .setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onCodeSent(@NonNull String verificationId, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) { // Save the verification id somewhere // ... // The corresponding whitelisted code above should be used to complete sign-in. MainActivity.this.enableUserManuallyInputCode(); } @Override public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) { // Sign in with the credential // ... } @Override public void onVerificationFailed(@NonNull FirebaseException e) { // ... } }) .build(); PhoneAuthProvider.verifyPhoneNumber(options);
Kotlin+KTX
val phoneNum = "+16505554567" val testVerificationCode = "123456" // Whenever verification is triggered with the whitelisted number, // provided it is not set for auto-retrieval, onCodeSent will be triggered. val options = PhoneAuthOptions.newBuilder(Firebase.auth) .setPhoneNumber(phoneNum) .setTimeout(30L, TimeUnit.SECONDS) .setActivity(this) .setCallbacks(object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() { override fun onCodeSent( verificationId: String, forceResendingToken: PhoneAuthProvider.ForceResendingToken, ) { // Save the verification id somewhere // ... // The corresponding whitelisted code above should be used to complete sign-in. this@MainActivity.enableUserManuallyInputCode() } override fun onVerificationCompleted(phoneAuthCredential: PhoneAuthCredential) { // Sign in with the credential // ... } override fun onVerificationFailed(e: FirebaseException) { // ... } }) .build() PhoneAuthProvider.verifyPhoneNumber(options)
此外,您可以透過呼叫 setAutoRetrievedSmsCodeForPhoneNumber
來設定虛構號碼及其對應的驗證碼,藉此在 Android 中測試自動擷取流程。
呼叫 verifyPhoneNumber
時,系統會直接透過 PhoneAuthCredential
觸發 onVerificationCompleted
。此功能只適用於虛構的電話號碼。
將應用程式發布到 Google Play 商店時,請務必停用該功能,且應用程式中不會以硬式編碼的方式編寫虛構的電話號碼。
Java
// The test phone number and code should be whitelisted in the console. String phoneNumber = "+16505554567"; String smsCode = "123456"; FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(); FirebaseAuthSettings firebaseAuthSettings = firebaseAuth.getFirebaseAuthSettings(); // Configure faking the auto-retrieval with the whitelisted numbers. firebaseAuthSettings.setAutoRetrievedSmsCodeForPhoneNumber(phoneNumber, smsCode); PhoneAuthOptions options = PhoneAuthOptions.newBuilder(firebaseAuth) .setPhoneNumber(phoneNumber) .setTimeout(60L, TimeUnit.SECONDS) .setActivity(this) .setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(@NonNull PhoneAuthCredential credential) { // Instant verification is applied and a credential is directly returned. // ... } // ... }) .build(); PhoneAuthProvider.verifyPhoneNumber(options);
Kotlin+KTX
// The test phone number and code should be whitelisted in the console. val phoneNumber = "+16505554567" val smsCode = "123456" val firebaseAuth = Firebase.auth val firebaseAuthSettings = firebaseAuth.firebaseAuthSettings // Configure faking the auto-retrieval with the whitelisted numbers. firebaseAuthSettings.setAutoRetrievedSmsCodeForPhoneNumber(phoneNumber, smsCode) val options = PhoneAuthOptions.newBuilder(firebaseAuth) .setPhoneNumber(phoneNumber) .setTimeout(60L, TimeUnit.SECONDS) .setActivity(this) .setCallbacks(object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() { override fun onVerificationCompleted(credential: PhoneAuthCredential) { // Instant verification is applied and a credential is directly returned. // ... } // ... }) .build() PhoneAuthProvider.verifyPhoneNumber(options)
後續步驟
使用者首次登入時,系統會建立新的使用者帳戶,並連結至憑證 (即使用者名稱與密碼、電話號碼或驗證提供者資訊),也就是使用者登入時使用的憑證。這個新帳戶會儲存在您的 Firebase 專案中,可用來識別專案中各個應用程式的使用者 (無論使用者登入方式為何)。
-
在應用程式中,您可以透過
FirebaseUser
物件取得使用者的基本個人資料。請參閱 管理使用者一文。 在 Firebase 即時資料庫和 Cloud Storage 安全性規則中,您可以透過
auth
變數取得登入使用者的專屬 ID,並使用該 ID 控管使用者可存取哪些資料。
您可以將驗證供應商憑證連結至現有的使用者帳戶,讓使用者透過多個驗證服務提供者登入您的應用程式。
如要登出使用者,請呼叫
signOut
:
Kotlin+KTX
Firebase.auth.signOut()
Java
FirebaseAuth.getInstance().signOut();