Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.melonfeed.melonfeed;
- 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.TextView;
- import android.widget.Toast;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.Task;
- import com.google.android.material.textfield.TextInputEditText;
- import com.google.firebase.FirebaseException;
- import com.google.firebase.auth.AuthResult;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.PhoneAuthCredential;
- import com.google.firebase.auth.PhoneAuthOptions;
- import com.google.firebase.auth.PhoneAuthProvider;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.concurrent.TimeUnit;
- public class otpVerification extends AppCompatActivity {
- Button confirm;
- String phoneNumber, verificationCode;
- long timeoutSeconds = 60L;
- PhoneAuthProvider.ForceResendingToken resendingToken;
- TextView resend;
- FirebaseAuth mAuth = FirebaseAuth.getInstance();
- TextInputEditText otp;
- String verificationID;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_otp_verification);
- confirm = findViewById(R.id.confirmBTN);
- otp = findViewById(R.id.editTextOTP);
- resend = findViewById(R.id.resendOTP);
- phoneNumber = getIntent().getStringExtra("phone");
- mAuth.getFirebaseAuthSettings().setAppVerificationDisabledForTesting(true);
- sendOtp(phoneNumber, false);
- confirm.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String enteredOtp = otp.getText().toString();
- PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationCode, enteredOtp);
- signIn(credential);
- }
- });
- resend.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- sendOtp(phoneNumber, true);
- }
- });
- }
- void sendOtp(String phoneNumber, boolean isResend) {
- startResendTimer();
- PhoneAuthOptions.Builder builder =
- PhoneAuthOptions.newBuilder(mAuth)
- .setPhoneNumber(phoneNumber)
- .setTimeout(timeoutSeconds, TimeUnit.SECONDS)
- .setActivity(this)
- .setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
- @Override
- public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
- signIn(phoneAuthCredential);
- }
- @Override
- public void onVerificationFailed(@NonNull FirebaseException e) {
- Toast.makeText(otpVerification.this, "OTP verification failed", Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
- super.onCodeSent(s, forceResendingToken);
- verificationCode = s;
- resendingToken = forceResendingToken;
- Toast.makeText(otpVerification.this, "OTP sent successfully", Toast.LENGTH_SHORT).show();
- }
- });
- if(isResend){
- PhoneAuthProvider.verifyPhoneNumber(builder.setForceResendingToken(resendingToken).build());
- }else{
- PhoneAuthProvider.verifyPhoneNumber(builder.build());
- }
- }
- void signIn(PhoneAuthCredential phoneAuthCredential){
- //login and go to next activity
- mAuth.signInWithCredential(phoneAuthCredential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if(task.isSuccessful()){
- Intent intent = new Intent(otpVerification.this,Settings.class);
- intent.putExtra("phone", phoneNumber);
- startActivity(intent);
- }else{
- Toast.makeText(otpVerification.this, "OTP verification failed", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- void startResendTimer() {
- resend.setEnabled(false);
- Timer timer = new Timer();
- timer.scheduleAtFixedRate(new TimerTask() {
- @Override
- public void run() {
- timeoutSeconds--;
- resend.setText("Resend OTP in " + timeoutSeconds + " seconds");
- if (timeoutSeconds <= 0) {
- timeoutSeconds = 60L;
- timer.cancel();
- runOnUiThread(() -> {
- resend.setEnabled(true);
- });
- }
- }
- }, 0, 1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment