phone authentication in android using Firebase
Step 2: Add this line build.Gradle(project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath 'com.google.gms:google-services:4.3.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Step 3: Add this dependency build.Gradle (app level)
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.ruhul.firebasephoneauth"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation platform('com.google.firebase:firebase-bom:27.1.0')
implementation 'com.google.firebase:firebase-analytics'
//firebase phonnumber auth andriod
implementation 'com.firebaseui:firebase-ui-auth:7.1.1'
}
apply plugin: 'com.google.gms.google-services'
Step 5: After that create signin_layout.xml: res/layout/signin_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@drawable/rid_bg"
android:layout_height="match_parent">
<com.google.android.material.button.MaterialButton
android:id="@+id/phone_login_id"
android:drawableLeft="@drawable/fui_ic_phone_white_24dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="150dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="sign in Phone"
android:backgroundTint="#00796B"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.google.android.material.button.MaterialButton>
</RelativeLayout>
Step 6: After that, you can now full code check
package com.ruhul.firebasephoneauth;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.firebase.ui.auth.AuthMethodPickerLayout;
import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.IdpResponse;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<AuthUI.IdpConfig> idpConfigList;
private int loging_request_code=1;
private FirebaseAuth mauth;
private FirebaseUser user;
private FirebaseAuth.AuthStateListener authStateListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
idpConfigList= Arrays.asList(
new AuthUI.IdpConfig.PhoneBuilder().build()
);
mauth=FirebaseAuth.getInstance();
authStateListener=new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
user=firebaseAuth.getCurrentUser();
if (user!=null)
{
gotohome();
}
else {
// user phone number not authentication
need_login();
}
}
};
}
private void need_login() {
AuthMethodPickerLayout authMethodPickerLayout=new AuthMethodPickerLayout
.Builder(R.layout.singin_layout)
.setPhoneButtonId(R.id.phone_login_id)
.build();
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAuthMethodPickerLayout(authMethodPickerLayout)
.setIsSmartLockEnabled(false)
.setTheme(R.style.LoginTheme)
.setAvailableProviders(idpConfigList)
.build(),
loging_request_code);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onStart() {
super.onStart();
mauth.addAuthStateListener(authStateListener);
}
@Override
protected void onStop() {
super.onStop();
if (mauth!=null && authStateListener!=null)
{
mauth.removeAuthStateListener(authStateListener);
}
}
private void gotohome() {
startActivity(new Intent(MainActivity.this,HomeActivity.class));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==loging_request_code)
{
IdpResponse idpResponse=IdpResponse.fromResultIntent(data);
if (resultCode==RESULT_OK)
{
Toast.makeText(this, "user authenticated", Toast.LENGTH_SHORT).show();
}
}
}
}
this tutorial work fine and clear topic clear code .thanks
ReplyDeletethanks for your valuable comment
ReplyDelete