Phone Number Verification using Firebase in Android

Configurare noua (How To)

Situatie

Phone number Firebase Authentication is used to sign in a user by sending an SMS message to the user’s phone. The user signs in using a one-time code contained in the SMS message.

Solutie

Pasi de urmat
  • Create a new project in Android Studio
  • Connect Project to Firebase

Lightbox

  • Add dependency to the build.gradle file and click “sync now”

implementation platform(‘com.google.firebase:firebase-bom:26.5.0’)

implementation ‘com.google.firebase:firebase-auth-ktx’

Create two new activities. One PhoneNumberActivity.kt with the layout file activity_phone_number.xml for entering the phone number and starting the authentication process. Second OtpActivity.kt with the layout file activity_otp.xmlfor entering OTP received from firebase.

Working with activity_phone_number.xml. Go to the activity_phone_number.xmlfile and write the following code.

<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.constraintlayout.widget.ConstraintLayout
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”
tools:context=”.PhoneNumberActivity”>
<!–This will be used to enter the phone number–>
<EditText
android:id=”@+id/et_phone_number”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginStart=”20dp”
android:layout_marginTop=”200dp”
android:layout_marginEnd=”20dp”
android:layout_marginBottom=”20dp”
android:ems=”10″
android:hint=”Enter Phone Number”
android:inputType=”phone”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toTopOf=”parent”
tools:layout_editor_absoluteY=”85dp” />
<!–On click of this buttton OTP will be send to phone–>
<Button
android:id=”@+id/button_otp”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”20dp”
android:text=”Send OTP”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toBottomOf=”@+id/et_phone_number” />
</androidx.constraintlayout.widget.ConstraintLayout>

Tip solutie

Permanent

Voteaza

(9 din 15 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?