首页 > 解决方案 > Firebase SMS 身份验证登录方法

问题描述

我有这个使用 SMS 代码登录 Firebase 的 Android 代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FirebaseApp.initializeApp(this);

        userLoggedIn();
        
        mPhoneNumber = findViewById(R.id.phoneNumber);
        mCode = findViewById(R.id.code);
        mSend = findViewById(R.id.send);

        mAuth = FirebaseAuth.getInstance();

        mSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mVerificationId != null) {
                    // verifyPhoneNumberWithCode(mVerificationId, mCode.getText().toString());
                    verifyPhoneNumberWithCode(mVerificationId, mCode.getText().toString());
                } else {
                    startPhoneNumberVerification();
                }
            }
        });

        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
                Log.d(TAG, "onVerificationCompleted:" + phoneAuthCredential);
                signInWithPhoneAuthCredentials(phoneAuthCredential);
            }

            @Override
            public void onVerificationFailed(@NonNull FirebaseException e) {

            }
            @Override
            public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                super.onCodeSent(verificationId, forceResendingToken);

                mVerificationId = verificationId;
                mSend.setText("Verify Code");
            }

        };
    }

    private void verifyPhoneNumberWithCode(String verificationId, String code) {
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
        signInWithPhoneAuthCredentials(credential);;
    }

    private void startPhoneNumberVerification() {

        PhoneAuthOptions options =
                PhoneAuthOptions.newBuilder(mAuth)
                        .setPhoneNumber(mPhoneNumber.getText().toString())       // Phone number to verify
                        .setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
                        .setActivity(this)                 // Activity (for callback binding)
                        .setCallbacks(mCallbacks)          // OnVerificationStateChangedCallbacks
                        .build();

        PhoneAuthProvider.verifyPhoneNumber(options);
    }

    private void signInWithPhoneAuthCredentials(PhoneAuthCredential phoneAuthCredential) {

        FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()) {
                    userIsLoggedIn();
                }
            }
        });
    }

    private void userIsLoggedIn() {
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if (user != null) {
            startActivity(new Intent(getApplicationContext(), MainPageActivity.class));
            finish();
            return;
        }
    }

代码运行良好,但在我在 firebase 项目控制台上添加以下 2 种登录方法后,该应用程序不再需要新用户验证 SMS 代码 - 意思是,新用户在获取代码后自动登录而无需打孔在应用程序的代码中。

Firebase 身份验证登录提供商控制台

Google
Google 登录会在您连接的 iOS 和网络应用程序上自动配置。要为您的 Android 应用设置 Google 登录,您需要在项目设置中为每个应用添加SHA1 指纹。

电子邮件/密码
允许用户使用他们的电子邮件地址和密码进行注册。我们的 SDK 还提供电子邮件地址验证、密码恢复和电子邮件地址更改原语。学到更多

我的项目中同时配置了 SHA-1 和 SHA-256。

知道为什么它会在不验证发送的 SMS 代码的情况下自动登录新用户吗?

标签: androidfirebasefirebase-authentication

解决方案


您的电话号码可能正在进行即时验证。我观察到,大多数时候自动检索都有效。

我猜这种行为可能是基于服务提供商/国家代码/设备或其他因素而发生的。

https://firebase.google.com/docs/auth/android/phone-auth#onverificationcompletedphoneauthcredential

onVerificationCompleted(PhoneAuthCredential)

该方法在两种情况下调用:

即时验证:在某些情况下,无需发送或输入验证码即可即时验证电话号码。

自动检索:在某些设备上,Google Play 服务可以自动检测传入的验证短信并执行验证,无需用户操作。(某些运营商可能无法使用此功能。)

无论哪种情况,用户的电话号码都已成功验证,您可以使用传递给回调的 PhoneAuthCredential 对象来登录用户。


推荐阅读