User Phone Number Verification through Firebase in Android Studio.



Add dependency to your "build.gradle " :
implementation 'com.google.firebase:firebase-database:16.0.4'
implementation 'com.google.firebase:firebase-storage:16.0.4'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.hbb20:ccp:2.3.2'
Add Custom Button to drawable: 
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#938709"/>
<corners android:radius="25dp"/>
</shape>
Add custom editText to drawable: 
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#9874a7"/>
<corners android:radius="25dp"/>
</shape>

activity_main.xml:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login"
tools:context=".MainActivity">
<TextView
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:gravity="center"
android:text="PHONE\nVERIFICATION"
android:textColor="#6638e2"
android:textSize="40dp"
android:textStyle="bold" />

<com.hbb20.CountryCodePicker
android:id="@+id/ccp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginStart="5dp"/>


<EditText
android:id="@+id/number1"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="5dp"
android:layout_marginTop="240dp"
android:background="@drawable/textedit_custom"
android:ems="10"
android:hint="PHONE NUMBER"
android:inputType="number"
android:padding="8dp"
android:paddingEnd="8dp" />

<Button
android:id="@+id/submitbtn"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginStart="5dp"
android:layout_marginTop="320dp"
android:layout_marginEnd="5dp"
android:background="@drawable/button_custom"
android:text="Send OTP" />

<EditText
android:id="@+id/otptext"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginStart="5dp"
android:layout_marginTop="430dp"
android:background="@drawable/textedit_custom"
android:ems="10"
android:hint="OTP"
android:inputType="number"
android:padding="8dp"
android:paddingEnd="5dp" />

<Button
android:id="@+id/verify"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="5dp"
android:layout_marginTop="505dp"
android:layout_marginEnd="5dp"
android:background="@drawable/button_custom"
android:text="verify"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<TextView
android:id="@+id/textresend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="51dp"
android:layout_marginBottom="109dp"
android:text="OTP not received? RESEND"
android:textColor="#6638e2"
android:textSize="20dp" />


</RelativeLayout>
MainActivity.java:
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.alumni.R;
import com.example.alumni.CreateAccount.RegisterationForm;
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 com.hbb20.CountryCodePicker;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

private EditText number1,otp;
private Button submit;
private CountryCodePicker codePicker;
private Button submitotp;
private TextView resend;
private ProgressDialog loadingbar;
String Phonenumber, id;
FirebaseAuth mAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify_user_phon_number);

number1=findViewById(R.id.number1);
submit=findViewById(R.id.submitbtn);
codePicker=findViewById(R.id.ccp);
codePicker.registerCarrierNumberEditText(number1);

loadingbar = new ProgressDialog(this);
otp = findViewById(R.id.otptext);
submitotp = findViewById(R.id.verify);
resend=findViewById(R.id.textresend);
mAuth = FirebaseAuth.getInstance();

otp.setVisibility(View.INVISIBLE);
resend.setVisibility(View.INVISIBLE);
submitotp.setVisibility(View.INVISIBLE);


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

String phone=
number1.getText().toString();
if(TextUtils.isEmpty(phone))
{
Toast.
makeText(VerifyUserPhonNumber.this, "please enter phone", Toast.LENGTH_SHORT).show();
}
else if (phone.replace(" ", "").length() != 10) {
Toast.
makeText(VerifyUserPhonNumber.this, "please enter valid phone", Toast.LENGTH_SHORT).show();
}
else
{
otp.setVisibility(View.VISIBLE);
resend.setVisibility(View.VISIBLE);
submitotp.setVisibility(View.VISIBLE);
Phonenumber=codePicker.getFullNumberWithPlus().replace(" ","");
sendVerificationCode();

}
}
})
;


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

String otpmsg =
otp.getText().toString();
if (TextUtils.isEmpty(otpmsg)) {
Toast.
makeText(VerifyUserPhonNumber.this, "please enter otp", Toast.LENGTH_SHORT).show();
} else if (otpmsg.replace(" ", "").length() != 6) {
Toast.
makeText(VerifyUserPhonNumber.this, "please enter valid otp", Toast.LENGTH_SHORT).show();
} else {
loadingbar.setTitle("creating account");
loadingbar.setMessage("please wait");
loadingbar.setCanceledOnTouchOutside(false);
loadingbar.show();
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(id, otpmsg);
signInWithPhoneAuthCredential(credential);
}
}
})
;
resend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendVerificationCode()
;
}
})
;
}

private void sendVerificationCode () {
new CountDownTimer(60000, 1000) {
@Override
public void onTick(long l) {

resend.setText("" + l / 1000);
resend.setEnabled(false);
}

@Override
public void onFinish() {
resend.setText("RESEND");
resend.setEnabled(true);

}
}.start()
;

PhoneAuthProvider.getInstance().verifyPhoneNumber(
Phonenumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

@Override
public void onCodeSent(@NonNull String id, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
VerifyUserPhonNumber.
this.id = id;
}

@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
signInWithPhoneAuthCredential(phoneAuthCredential)
;
}

@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
Toast.
makeText(VerifyUserPhonNumber.this, "FAILED", Toast.LENGTH_SHORT).show();

}


})
; // OnVerificationStateChangedCallbacks*/

}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {

mAuth.signInWithCredential(credential)
.addOnCompleteListener(
this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
loadingbar.dismiss();
if (task.isSuccessful()) {
Intent intent=
new Intent(VerifyUserPhonNumber.this, RegisterationForm.class);
intent.putExtra("phoneNumber",Phonenumber);
startActivity(intent);
finish();
FirebaseUser user = task.getResult().getUser();

}
else {
Toast.
makeText(VerifyUserPhonNumber.this, "VERIFICATION FAILED", Toast.LENGTH_SHORT).show();

}
}
})
;
}
}

Comments

Popular posts from this blog

Introduction to Python.

Decision Making Statement.