如何使Firebase电话身份验证适用于我的应用程序?

时间:2018-08-22 12:04:50

标签: java android firebase firebase-authentication

不确定我在做什么错,我已经在Stack Overflow上搜索并阅读了文章和观看了视频,但仍然没有解决方法。

基本上,我想通过Firebase向用户的电话发送一条SMS消息以验证电话号码。我的应用程序不在Play商店或类似的设备中,我只是通过使用USB电缆将手机连接到PC来运行它。

据我所知我已经正确设置了。这是我在Android Studio中看到的:

enter image description here

在FireBase中: enter image description here

enter image description here

这是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity  {

    EditText editTextPhone, editTextCode;

    FirebaseAuth mAuth;

    String codeSent;

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

        mAuth = FirebaseAuth.getInstance();

        editTextCode = findViewById(R.id.editTextCode);
        editTextPhone = findViewById(R.id.editTextPhone);

        findViewById(R.id.buttonGetVerificationCode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendVerificationCode();
            }
        });


        findViewById(R.id.buttonSignIn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verifySignInCode();
            }
        });
    }

    private void verifySignInCode(){
        String code = editTextCode.getText().toString();
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, code);
        signInWithPhoneAuthCredential(credential);
    }

    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        //here you can open new activity
                        Toast.makeText(getApplicationContext(),
                            "Login Successfull", Toast.LENGTH_LONG).show();
                    } else {
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            Toast.makeText(getApplicationContext(),
                                "Incorrect Verification Code ", Toast.LENGTH_LONG).show();
                        }
                    }
                }
            });
    }

    private void sendVerificationCode(){

        String phone = editTextPhone.getText().toString();

        if(phone.isEmpty()){
            editTextPhone.setError("Phone number is required");
            editTextPhone.requestFocus();
            return;
        }

        if(phone.length() < 10 ){
            editTextPhone.setError("Please enter a valid phone");
            editTextPhone.requestFocus();
            return;
        }

        System.out.println("code should be sent");

        PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phone,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);        // OnVerificationStateChangedCallbacks
    }



    PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        @Override
        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

        }

        @Override
        public void onVerificationFailed(FirebaseException e) {

        }

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

            codeSent = s;
        }
    };


}

当我单击“获取验证”按钮时,我在日志中看到System.out.println,“应该发送代码”,因此按钮可以正常工作,只是没有代码被发送。有什么想法吗?

此外,我相信我已经正确设置了SHA-1。我是从Android Studio中获得的,方法是从Gradle选项卡中单击“ signingReport”,然后将数字复制到Firebase控制台,如下所示。

enter image description here

4 个答案:

答案 0 :(得分:1)

带国家/地区代码的密码,例如+ 91-(10位数字)

答案 1 :(得分:0)

这里看起来一切都很好。 我认为问题出在手机输入字段,即editTextPhone,因为firebase使用

的格式

[国家/地区代码10位数字移动电话号码]

例如印度+91 9090102030

因此,您应该抛出异常或将其打印在 onVerificationFailed 方法的日志部分。

答案 2 :(得分:0)

我的代码基于该视频,我从推荐的github帐户复制并粘贴了源代码:

https://www.youtube.com/watch?v=N6HBStmDkMQ

一切在我的Firebase控制台上看起来都不错,并且源代码正在加载并且没有错误-与我发送的SMS无关-因此,我从该项目复制并粘贴了代码,而无需更改任何FireBase设置:

https://www.simplifiedcoding.net/firebase-phone-authentication-android-tutorial/

现在它正在发送SMS消息。

答案 3 :(得分:0)

您必须提供国家/地区代码。 1-删除10位数字的支票以允许使用国家/地区代码。 2-印度为+91。 3-或者您可以为国家/地区代码添加另一个编辑文本字段。 4-为简短起见,您可以转到此链接http://blogssolutions.co.in/add-firebase-mobile-authentication-android