The SMS Code is expired. How to get pass this exception been stuck on this for days

Viewed 126

I keep on getting the error of sms code has been expiraed please resend it even though it just came in my phone. This for android studio firebase authentication by phone number by the way. Can we solve this error?

Below are the codes: The Send OTP Code:

public void sendOTP(){
    final String Mobile = getMobileNo();
    //Looks if the mobile number is already signed
    FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
    if(currentUser!=null){
        Toast.makeText(this,"User with mobile no"+Mobile+" already exists",Toast.LENGTH_LONG).show();
        Intent in = new Intent(OTPActivity.this,LoginActivity.class);
        in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(in);
    }
    //if not a current user then send otp.
    else {
        PhoneAuthOptions options = PhoneAuthOptions.newBuilder(mAuth)
                .setPhoneNumber(Mobile)
                .setTimeout(120L, TimeUnit.SECONDS)
                .setActivity(this)
                .setCallbacks(mCallBack)
                .build();
        PhoneAuthProvider.verifyPhoneNumber(options);
    }
}

The mCallBacks Code:

private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            super.onCodeSent(s, forceResendingToken);
            verificationId = s;
            setVerificationId(verificationId);
            setToken(forceResendingToken);
        }

    @Override
    public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
        final String code = phoneAuthCredential.getSmsCode();
        if(code!=null){
            OTP.setText(code);
            verifyCode(getVerificationId(),code);
        }
    }

    @Override
    public void onVerificationFailed(@NonNull FirebaseException e) {
        Toast.makeText(OTPActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
    }
};

The Verify Code:

public void verifyCode(String verificationId,String code){
        if(code!=null) {
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
        FirebaseAuth.getInstance().signInWithCredential(credential)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            FirebaseUser user = task.getResult().getUser();
                            AlertDialog.Builder builder = new AlertDialog.Builder(OTPActivity.this);
                            builder.setMessage("Congratulations!!You are Registered. Do You Want To Login?");
                            builder.setCancelable(true);
                            builder.setPositiveButton(
                                    "Yes",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            Intent in = new Intent(OTPActivity.this, LoginActivity.class);
                                            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                            startActivity(in);
                                        }
                                    });
                            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(OTPActivity.this, "Exiting the app.", Toast.LENGTH_SHORT).show();
                                    System.exit(0);
                                }
                            });
                        }
                        else
                        {
                            if(task.getException() instanceof FirebaseAuthInvalidCredentialsException){
                                Toast.makeText(OTPActivity.this,task.getException().getMessage(),Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });
    }
    else
    {
        Toast.makeText(this,"Code Field is Empty.Please Enter OTP.",Toast.LENGTH_LONG).show();
    }
}

Please Help. Been stuck on this for days.

The Exception I am getting is that: The sms Code has expired(even though I have got the otp code like seconds ago.).Please resend the code.

0 Answers
Related