Ошибка проверки подлинности телефона firebase - PullRequest
0 голосов
/ 07 мая 2020

Я действительно новичок в Firebase и раньше пытался создать страницу аутентификации по телефону, она работала нормально, но после того, как я создал макет реестра и связал его с этим макетом, он всегда не прошел проверку. Я включил все необходимые зависимости. Страница регистрации просто собирает имя пользователя, номер телефона, адрес электронной почты и помещает в базу данных firebase.

заранее спасибо !!

может кто-нибудь сказать мне, что пошло не так.

моя проверка код макета

package kanti.kushal.team_beta;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;

import com.google.firebase.FirebaseException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;

import java.util.concurrent.TimeUnit;

public class verifyphoneActivity extends AppCompatActivity {


    private FirebaseAuth mAuth;
    private FirebaseUser mcurrentUser;
    private String verificationid = null;
    Button btn_verifyotp;
    EditText txt_code;
    ProgressBar progress_Bar;
    String phone;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verifyphone);
        txt_code =findViewById(R.id.verifycode);

        //sent verification code
        phone = getIntent().getStringExtra("phoneme");
        send_VerificationCode(phone);



        mAuth = FirebaseAuth.getInstance();
        btn_verifyotp = findViewById(R.id.verifyotp);
        mcurrentUser = mAuth.getCurrentUser();
        progress_Bar =findViewById(R.id.progress_bar);


       btn_verifyotp.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {

             String code =txt_code.getText().toString();
             if(code.length()==6) {
                 verifycode(code);
             }
             else{
                 Toast.makeText(verifyphoneActivity.this, "Enter correct OTP", Toast.LENGTH_SHORT).show();
             }

           }
       });


    }

    private void verifycode(String code_txt){
      PhoneAuthCredential credential =PhoneAuthProvider.getCredential(verificationid,code_txt);
      signInwithCredentail(credential);

    }

    private void signInwithCredentail(PhoneAuthCredential credential) {

        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        if(task.isSuccessful()){

                            Intent intent = new Intent(verifyphoneActivity.this,RegisterActivity.class);
                            intent.putExtra("ph_no",phone);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);

                        }
                        else{
                            Toast.makeText(verifyphoneActivity.this,task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        }

                    }
                });
    }


    private void send_VerificationCode(String ph_no){

        PhoneAuthProvider.getInstance()
                .verifyPhoneNumber(
                        ph_no,
                        60,
                        TimeUnit.SECONDS,
                        this,
                        mCallback);

    }

    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallback = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            super.onCodeSent(s, forceResendingToken);
            verificationid = s;
        }

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

            String code = phoneAuthCredential.getSmsCode();
            if(code!=null){
                txt_code.setText(code);
                 verifycode(code);
            }

        }

        @Override
        public void onVerificationFailed(@NonNull FirebaseException e) {
            Toast.makeText(verifyphoneActivity.this, "Verification Failed. Try Again...", Toast.LENGTH_SHORT).show();
        }
    };


    @Override
    public void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.
        if(mcurrentUser != null)
        {
            Intent intent = new Intent(verifyphoneActivity.this,MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
        }
    }
}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...