您可以使用 Firebase Authentication 傳送簡訊到使用者的手機,讓使用者登入。使用者使用簡訊中的一次性驗證碼登入。
如要在應用程式中加入電話號碼登入功能,最簡單的方法是使用 FirebaseUI,其中包含置入式登入小工具,可實作電話號碼登入、密碼登入和聯合登入的登入流程。本文說明如何使用 Firebase SDK 導入電話號碼登入流程。
事前準備
- 如果您尚未將 Firebase 新增至 Android 專案,請先新增。
-
在模組 (應用程式層級) Gradle 檔案 (通常是
<project>/<app-module>/build.gradle.kts或<project>/<app-module>/build.gradle) 中,新增 Android Firebase Authentication 程式庫的依附元件。建議使用 Firebase Android BoM 控制程式庫版本管理。dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:35.1.0")) // 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:25.0.0") }
- 如果尚未將應用程式連結至 Firebase 專案,請前往 Firebase 控制台進行連結。
- 如果您尚未在 Firebase 控制台中設定應用程式的 SHA-1 雜湊,請完成這項操作。如要瞭解如何找出應用程式的 SHA-1 雜湊,請參閱「 驗證用戶端」。
安全疑慮
雖然僅使用電話號碼進行驗證很方便,但安全性不如其他可用方法,因為電話號碼的擁有權很容易在使用者之間轉移。此外,在具有多個使用者設定檔的裝置上,任何可接收簡訊的使用者都可以使用裝置的電話號碼登入帳戶。
如果應用程式支援以電話號碼登入,請同時提供更安全的登入方式,並告知使用者以電話號碼登入的安全風險。
為 Firebase 專案啟用電話號碼登入功能
如要讓使用者透過簡訊登入,您必須先為 Firebase 專案啟用電話號碼登入方式:
- 在 Firebase 控制台中,依序前往「Security」(安全性) >「Authentication」(驗證)。
- 在「登入方式」分頁中,啟用「電話」登入供應商。
-
設定政策,允許或禁止傳送簡訊至特定區域。設定簡訊區域政策,有助於防範應用程式遭到簡訊濫用。如果是新專案,預設政策不允許任何區域。
- 在 Firebase 控制台中,依序前往「Security」(安全性) >「Authentication」(驗證) >「Settings」(設定) 分頁標籤。
- 在「簡訊區域政策」部分,設定簡訊區域政策。
啟用應用程式驗證功能
如要使用電話號碼驗證功能,Firebase 必須能夠驗證電話號碼登入要求是否來自您的應用程式。Firebase Authentication 有三種方法可以做到這一點:
- Play Integrity API:如果使用者裝置上已安裝 Google Play services,且 Firebase Authentication 可透過 Play Integrity API 驗證裝置是否合法,則可繼續進行電話號碼登入程序。Play Integrity API 是在 Google 擁有的專案 (Firebase Authentication) 中啟用,而非您的專案。這不會計入專案的 Play Integrity API 配額。Authentication SDK 21.2.0 以上版本 (Firebase BoM 31.4.0 以上版本) 支援 Play Integrity。
如要使用 Play Integrity,請指定應用程式的 SHA-256 指紋 (如果尚未指定)。
-
在 Firebase 控制台中,依序前往「
Settings 」(設定) >「General tab」(一般分頁)。 - 向下捲動至「您的應用程式」資訊卡,選取 Android 應用程式,然後在「SHA 憑證指紋」欄位中新增 SHA-256 指紋。
如要瞭解如何取得應用程式的 SHA 指紋,請參閱「驗證用戶端」。
-
在 Firebase 控制台中,依序前往「
- reCAPTCHA 驗證:如果無法使用 Play Integrity,例如使用者的裝置未安裝 Google Play services,Firebase Authentication 會使用 reCAPTCHA 驗證完成電話號碼登入流程。使用者通常不必回答任何問題,就能完成 reCAPTCHA 驗證問題。請注意,這個流程需要將 SHA-1 與應用程式建立關聯。此外,這個流程也需要 API 金鑰不受限制,或已加入
PROJECT_ID.firebaseapp.com的許可清單。觸發 reCAPTCHA 的部分情況:
- 使用者的裝置未安裝 Google Play services。
- 應用程式並非透過 Google Play Store 發行 (Authentication SDK 21.2.0 以上版本)。
- 取得的 SafetyNet 權杖無效 (Authentication SDK 版本低於 21.2.0)。
使用 SafetyNet 或 Play Integrity 驗證應用程式時,系統會根據 Google Play Store,在簡訊範本的
%APP_NAME%欄位填入應用程式名稱。在觸發 reCAPTCHA 的情境中,%APP_NAME%會填入PROJECT_ID.firebaseapp.com。
forceRecaptchaFlowForTesting 強制執行 reCAPTCHA 驗證流程。
您可以使用 setAppVerificationDisabledForTesting 停用應用程式驗證 (使用虛構電話號碼時)。
疑難排解
使用 reCAPTCHA 驗證應用程式時,出現「Missing initial state」錯誤
如果 reCAPTCHA 流程順利完成,但未將使用者重新導向至原生應用程式,就可能發生這種情況。如果發生這種情況,系統會將使用者重新導向至備用網址
PROJECT_ID.firebaseapp.com/__/auth/handler。在 Firefox 瀏覽器中,系統預設會停用開啟原生應用程式連結的功能。如果 Firefox 顯示上述錯誤,請按照「設定 Firefox for Android 在原生應用程式中開啟連結」一文的步驟操作,啟用開啟應用程式連結的功能。
將驗證碼傳送至使用者的手機
如要啟動電話號碼登入功能,請向使用者顯示介面,提示他們輸入電話號碼。法律要求不盡相同,但為確保使用者瞭解相關規定,最佳做法是告知他們,如果使用電話號碼登入,可能會收到驗證簡訊,且須支付標準費率。
接著,將使用者的電話號碼傳遞至 PhoneAuthProvider.verifyPhoneNumber 方法,要求 Firebase 驗證使用者的電話號碼。例如:
Kotlin
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 後,請設定標記,指出驗證正在進行中。接著,在 Activity 的 onSaveInstanceState 方法中儲存標記,並在 onRestoreInstanceState 中還原標記。最後,在 Activity 的 onStart 方法中,檢查驗證是否已在進行中,如果是,請再次呼叫 verifyPhoneNumber。驗證完成或失敗時,請務必清除標記 (請參閱「
驗證回呼」)。
如要輕鬆處理螢幕旋轉和其他 Activity 重新啟動例項,請將 Activity 傳遞至 verifyPhoneNumber 方法。當 Activity 停止時,回呼會自動卸離,因此您可以在回呼方法中自由編寫 UI 轉換程式碼。
Firebase 傳送的簡訊也可以本地化,只要在 Auth 執行個體上透過 setLanguageCode 方法指定驗證語言即可。
Kotlin
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
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 卡的裝置上,系統會立即呼叫這個方法,因為無法自動擷取簡訊。
部分應用程式會封鎖使用者輸入內容,直到自動驗證逾時,然後才顯示 UI,提示使用者輸入簡訊中的驗證碼 (不建議)。
建立 PhoneAuthCredential 物件
使用者輸入 Firebase 傳送至手機的驗證碼後,請使用驗證碼和傳遞至 onCodeSent 或 onCodeAutoRetrievalTimeOut 回呼的驗證 ID,建立 PhoneAuthCredential 物件。(呼叫 onVerificationCompleted 時,您會直接取得 PhoneAuthCredential 物件,因此可以略過這個步驟)。
如要建立 PhoneAuthCredential 物件,請呼叫 PhoneAuthProvider.getCredential:
Kotlin
val credential = PhoneAuthProvider.getCredential(verificationId!!, code)
Java
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
登入使用者帳戶
取得 PhoneAuthCredential 物件後 (無論是在 onVerificationCompleted 回呼中,還是透過呼叫 PhoneAuthProvider.getCredential),請將 PhoneAuthCredential 物件傳遞至 FirebaseAuth.signInWithCredential,完成登入流程:
Kotlin
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 Emulator 中開發。
- 編寫整合測試,不受正式環境中通常套用至實際電話號碼的安全檢查阻礙。
虛構電話號碼必須符合下列規定:
- 請務必使用虛構且不存在的電話號碼。 Firebase Authentication 不允許將實際使用者現有的電話號碼設為測試號碼。其中一個做法是使用 555 前置字元號碼做為美國測試電話號碼,例如:+1 650-555-3434
- 電話號碼的長度和其他限制必須符合正確格式。系統仍會像驗證真實使用者的電話號碼一樣,對這些號碼進行驗證。
- 最多可新增 10 組電話號碼用於開發。
- 使用難以猜測的測試電話號碼/驗證碼,並經常變更。
建立虛構的電話號碼和驗證碼
- 在 Firebase 控制台中,依序前往 「Security」(安全性) >「Authentication(驗證)」。
- 在「登入方式」分頁中,啟用「電話」登入供應商 (如果尚未啟用)。
- 展開「測試用電話號碼」部分。
-
提供要測試的電話號碼,例如:
+1 650-555-3434。 -
提供該號碼的 6 位數驗證碼,例如:
654321。 - 為每組號碼按一下「新增」。如有需要,您可以將滑鼠游標懸停在對應的資料列上,然後按一下垃圾桶圖示,刪除電話號碼和代碼。
手動測試
您可以直接在應用程式中使用虛構的電話號碼。這樣一來,您就能在開發階段進行手動測試,不必擔心配額問題或節流。您也可以直接透過 iOS 模擬器或 Android Emulator 進行測試,不必安裝 Google Play 服務。
提供虛構電話號碼並傳送驗證碼時,系統不會傳送實際簡訊。而是需要提供先前設定的驗證碼,才能完成登入。
登入完成後,系統會使用該電話號碼建立 Firebase 使用者。該使用者的行為和屬性與實際電話號碼使用者相同,並能以相同方式存取 Realtime Database/Cloud Firestore 和其他服務。在這個程序中產生的 ID 權杖,簽章與實際電話號碼使用者的簽章相同。
另一個做法是透過自訂宣告為這些使用者設定測試角色,將他們區分為假使用者,進一步限制存取權。
如要手動觸發 reCAPTCHA 流程進行測試,請使用 forceRecaptchaFlowForTesting() 方法。
// Force reCAPTCHA flow FirebaseAuth.getInstance().getFirebaseAuthSettings().forceRecaptchaFlowForTesting();
整合測試
除了手動測試外,Firebase Authentication 也提供 API,協助您編寫電話驗證整合測試。這些 API 會停用網頁中的 reCAPTCHA 需求,以及 iOS 中的無聲推播通知,藉此停用應用程式驗證。這樣一來,您就能在這些流程中進行自動化測試,而且更容易實作。此外,您也可以在 Android 上測試即時驗證流程。
在 Android 上,請先呼叫 setAppVerificationDisabledForTesting(),再呼叫 signInWithPhoneNumber。這會自動停用應用程式驗證,讓您不必手動解決,就能傳遞電話號碼。即使 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
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)
此外,您也可以在 Android 中測試自動擷取流程,方法是呼叫 setAutoRetrievedSmsCodeForPhoneNumber,設定虛構號碼和對應的自動擷取驗證碼。
呼叫 verifyPhoneNumber 時,系統會直接觸發 onVerificationCompleted 和 PhoneAuthCredential。這項功能僅適用於虛構的電話號碼。
將應用程式發布至 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
// 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 Realtime Database 和 Cloud Storage 安全性規則中,您可以從
auth變數取得已登入使用者的專屬使用者 ID,並用來控管使用者可存取的資料。
您可以將驗證供應商憑證連結至現有使用者帳戶,允許使用者透過多個驗證供應商登入應用程式。
如要登出使用者,請呼叫
signOut:
Kotlin
Firebase.auth.signOut()
Java
FirebaseAuth.getInstance().signOut();