簡體   English   中英

如何在 Android 的 Firebase 身份驗證中更改登錄電話號碼?

[英]How to change the Signed in Phone Number in Firebase Authentication for Android?

我正在使用Android Firebase Auth Ui在 Android 應用程序中提供基於電話號碼的登錄選項。 我試圖為登錄用戶提供一個額外的選項,將他們的登錄電話號碼切換到另一個保持相同用戶帳戶的號碼。

但是根據電話號碼的 Firebase Docs,沒有更改登錄號碼的選項。

可以選擇將不同的身份驗證提供商(如電子郵件、谷歌或 Facebook 登錄等)鏈接到同一帳戶。 但是沒有提到如何更改電話號碼或電子郵件 ID 保持相同的用戶 ID。

是否有解決方法或方法可以實現這一目標?

存在用於更新當前用戶電話號碼的 API: FirebaseUser#updatePhoneNumber(PhoneAuthCredential credential)

    val options = PhoneAuthOptions.newBuilder(FirebaseAuth.getInstance())
            .setPhoneNumber(phoneNumber) // Phone number to verify
            .setTimeout(100L, TimeUnit.SECONDS) // Timeout and unit
            .setActivity(activity) // Activity (for callback binding)
            .setCallbacks(returnCallBack()) // OnVerificationStateChangedCallbacks
            .build()

 PhoneAuthProvider.verifyPhoneNumber(options)

private fun returnCallBack() = object : PhoneAuthProvider
    .OnVerificationStateChangedCallbacks() {

        override fun onVerificationCompleted(credential: PhoneAuthCredential) {
            FirebaseAuth.getCurrentUser()?.updatePhoneNumber(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.e("phone",  e.toString())

        }

        override fun onCodeSent(verificationId: String, token: PhoneAuthProvider.ForceResendingToken) {
            //You need this to pass as a parameter for the update method call.
            vericationSent = verificationId
        }
    }   

 fun confirmChange(code: String, context: Context?) {
        if(code.contains(Regex(onlyNumber))) {
            Log.d("codeSent" , "Right code : $code")

            FirebaseAuth.getCurrentUser()
                ?.updatePhoneNumber(PhoneAuthProvider.getCredential(vericationSent, code))
                ?.addOnCompleteListener {task ->
                    //it worked if you reach here.

                }?.addOnFailureListener {
                    //Show the error to user
                    }
                }

            vericationSent = EMPTY
        } else {
            Log.d("codeSent" , "wrong code : $code")
        }

    }

我也遇到了更新用戶電話號碼的挑戰,當我繼續編寫文檔時,我通過使用我已經完成了這項任務得到了一些東西。

您可以點擊此處獲取文檔

現在您可以使用的方法: - 用於 java android 項目。

PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential( "+91-98298XXXX2", "OTP_CODE" );
// Update Mobile Number...
firebaseAuth.getCurrentUser().updatePhoneNumber(phoneAuthCredential)
    .addOnCompleteListener(new OnCompleteListener <Void>() {
        @Override
        public void onComplete(@NonNull Task <Void> task) {
            if (task.isSuccessful()) {
                // Update Successfully
            } else {
                // Failed
            }
        }
    }
);

嘗試這個

//Send otp to phone number

String verificationId;
private void startLoginFirebase(){
    PhoneAuthProvider.getInstance(firebaseAuth).verifyPhoneNumber(phone, 90L, TimeUnit.SECONDS, PhoneAuthActivity.this, new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            super.onCodeSent(s, forceResendingToken);
            verificationId = s;
            updatePhoneNum();
        }

        @Override
        public void onCodeAutoRetrievalTimeOut(@NonNull String s) {
            super.onCodeAutoRetrievalTimeOut(s);
        }

        @Override
        public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {

        }

        @Override
        public void onVerificationFailed(@NonNull FirebaseException e) {
            processFurther(e.getLocalizedMessage().toString(), 0);
        }
    });
}


//Verify Otp

private void updatePhoneNum(){
    PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(verificationId, otp);
    firebaseAuth.getCurrentUser().updatePhoneNumber(phoneAuthCredential).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {

        }
    });
}

顯然 - 根據項目維護者的說法 - FirebaseUI-Android不支持此功能,而且他們似乎沒有計划在近期內這樣做:(

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM