Guest User

Untitled

a guest
Sep 13th, 2023
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. package com.melonfeed.melonfeed;
  2.  
  3. import androidx.annotation.NonNull;
  4. import androidx.appcompat.app.AppCompatActivity;
  5.  
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import com.google.android.gms.tasks.OnCompleteListener;
  14. import com.google.android.gms.tasks.Task;
  15. import com.google.android.material.textfield.TextInputEditText;
  16. import com.google.firebase.FirebaseException;
  17. import com.google.firebase.auth.AuthResult;
  18. import com.google.firebase.auth.FirebaseAuth;
  19. import com.google.firebase.auth.PhoneAuthCredential;
  20. import com.google.firebase.auth.PhoneAuthOptions;
  21. import com.google.firebase.auth.PhoneAuthProvider;
  22.  
  23. import java.util.Timer;
  24. import java.util.TimerTask;
  25. import java.util.concurrent.TimeUnit;
  26.  
  27. public class otpVerification extends AppCompatActivity {
  28. Button confirm;
  29. String phoneNumber, verificationCode;
  30. long timeoutSeconds = 60L;
  31. PhoneAuthProvider.ForceResendingToken resendingToken;
  32. TextView resend;
  33. FirebaseAuth mAuth = FirebaseAuth.getInstance();
  34. TextInputEditText otp;
  35. String verificationID;
  36.  
  37. @Override
  38. protected void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.activity_otp_verification);
  41.  
  42. confirm = findViewById(R.id.confirmBTN);
  43. otp = findViewById(R.id.editTextOTP);
  44. resend = findViewById(R.id.resendOTP);
  45. phoneNumber = getIntent().getStringExtra("phone");
  46. mAuth.getFirebaseAuthSettings().setAppVerificationDisabledForTesting(true);
  47. sendOtp(phoneNumber, false);
  48.  
  49. confirm.setOnClickListener(new View.OnClickListener() {
  50. @Override
  51. public void onClick(View v) {
  52. String enteredOtp = otp.getText().toString();
  53. PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationCode, enteredOtp);
  54. signIn(credential);
  55. }
  56. });
  57.  
  58. resend.setOnClickListener(new View.OnClickListener() {
  59. @Override
  60. public void onClick(View v) {
  61. sendOtp(phoneNumber, true);
  62. }
  63. });
  64. }
  65.  
  66. void sendOtp(String phoneNumber, boolean isResend) {
  67. startResendTimer();
  68. PhoneAuthOptions.Builder builder =
  69. PhoneAuthOptions.newBuilder(mAuth)
  70. .setPhoneNumber(phoneNumber)
  71. .setTimeout(timeoutSeconds, TimeUnit.SECONDS)
  72. .setActivity(this)
  73. .setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
  74. @Override
  75. public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
  76. signIn(phoneAuthCredential);
  77. }
  78.  
  79. @Override
  80. public void onVerificationFailed(@NonNull FirebaseException e) {
  81. Toast.makeText(otpVerification.this, "OTP verification failed", Toast.LENGTH_SHORT).show();
  82. }
  83.  
  84. @Override
  85. public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
  86. super.onCodeSent(s, forceResendingToken);
  87. verificationCode = s;
  88. resendingToken = forceResendingToken;
  89. Toast.makeText(otpVerification.this, "OTP sent successfully", Toast.LENGTH_SHORT).show();
  90.  
  91. }
  92. });
  93. if(isResend){
  94. PhoneAuthProvider.verifyPhoneNumber(builder.setForceResendingToken(resendingToken).build());
  95. }else{
  96. PhoneAuthProvider.verifyPhoneNumber(builder.build());
  97. }
  98. }
  99.  
  100. void signIn(PhoneAuthCredential phoneAuthCredential){
  101. //login and go to next activity
  102. mAuth.signInWithCredential(phoneAuthCredential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
  103. @Override
  104. public void onComplete(@NonNull Task<AuthResult> task) {
  105. if(task.isSuccessful()){
  106. Intent intent = new Intent(otpVerification.this,Settings.class);
  107. intent.putExtra("phone", phoneNumber);
  108. startActivity(intent);
  109. }else{
  110. Toast.makeText(otpVerification.this, "OTP verification failed", Toast.LENGTH_SHORT).show();
  111. }
  112. }
  113. });
  114.  
  115.  
  116. }
  117.  
  118. void startResendTimer() {
  119. resend.setEnabled(false);
  120. Timer timer = new Timer();
  121. timer.scheduleAtFixedRate(new TimerTask() {
  122. @Override
  123. public void run() {
  124. timeoutSeconds--;
  125. resend.setText("Resend OTP in " + timeoutSeconds + " seconds");
  126. if (timeoutSeconds <= 0) {
  127. timeoutSeconds = 60L;
  128. timer.cancel();
  129. runOnUiThread(() -> {
  130. resend.setEnabled(true);
  131. });
  132. }
  133. }
  134. }, 0, 1000);
  135. }
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment