登录成功,无需验证码

时间:2019-05-19 10:55:05

标签: android firebase firebase-authentication

我正在通过firebase进行用户注册,就像通过电话号码进行whatsapp注册一样,我想要验证码来验证用户电话号码,然后验证接收到的代码。我收到验证码后,却成功登录,没有输入验证码,也没有验证验证码。

String phoneNumber = InputPhoneNumber.getText().toString();
            if (TextUtils.isEmpty(phoneNumber)) {
                Toast.makeText(MainActivity.this, "please enter your phone number first...", Toast.LENGTH_SHORT).show();
            } else {
                loadingBar.setTitle("Phone Verfication");
                loadingBar.setMessage("please wait, while we are authenticating your phone...");
                loadingBar.setCanceledOnTouchOutside(false);
                loadingBar.show();

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


        }

    });
VerifyButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SendVerificationCodeButton.setVisibility(View.INVISIBLE);
            InputPhoneNumber.setVisibility(View.INVISIBLE);

            String verificationCode =  InputVerifcationCode.getText().toString();

            if(TextUtils.isEmpty(verificationCode))
            {
                Toast.makeText(MainActivity.this, "please write verification code first...", Toast.LENGTH_SHORT).show();
            }
            else
            {
                loadingBar.setTitle("Verfication Code");
                loadingBar.setMessage("please wait, while we are verifying verification code...");
                loadingBar.setCanceledOnTouchOutside(false);
                loadingBar.show();

                PhoneAuthCredential credential = PhoneAuthProvider.getCredential( mVerificationId, verificationCode);
                signInWithPhoneAuthCredential(credential);
            }
        }
    });
callbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
            signInWithPhoneAuthCredential(phoneAuthCredential);

        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            loadingBar.dismiss();
            Toast.makeText(MainActivity.this, "Invalid phone number please enter correct number with your country code...", Toast.LENGTH_SHORT).show();

            SendVerificationCodeButton.setVisibility(View.VISIBLE);
            InputPhoneNumber.setVisibility(View.VISIBLE);
            VerifyButton.setVisibility(View.INVISIBLE);
            InputVerifcationCode.setVisibility(View.INVISIBLE);

        }
        @Override
        public void onCodeSent(String verificationId,
                               PhoneAuthProvider.ForceResendingToken token) {

            mVerificationId = verificationId;
            mResendToken = token;
            loadingBar.dismiss();

            Toast.makeText(MainActivity.this, "code has been sent, please check and verify", Toast.LENGTH_SHORT).show();

            SendVerificationCodeButton.setVisibility(View.INVISIBLE);
            InputPhoneNumber.setVisibility(View.INVISIBLE);
            VerifyButton.setVisibility(View.VISIBLE);
            InputVerifcationCode.setVisibility(View.VISIBLE);


        }
    };
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        String currentUserId = mAuth.getCurrentUser().getUid();
                        RootRef.child("User").child(currentUserId).setValue("");
                        loadingBar.dismiss();
                        Toast.makeText(MainActivity.this, "Congratulations, you are logged in successfully", Toast.LENGTH_SHORT).show();

                    } else {
                        String message = task.getException().toString();
                        Toast.makeText(MainActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
                    }

                }
            });
    }


}

0 个答案:

没有答案
相关问题