Skip to main content

 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 4: Add style  LogingTheme themes.xml:   res/values/themes/themes.xml 

<style name="LoginTheme" parent="FirebaseUI">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorButtonNormal">@color/colorAccent</item>
<item name="colorControlNormal">@android:color/white</item>
<item name="colorControlActivated">@android:color/black</item>
<item name="colorControlHighlight">@android:color/black</item>
<item name="android:textColor">@android:color/black</item>
<item name="android:textColorPrimary">@android:color/black</item>
<item name="android:textColorSecondary">@android:color/black</item>
<item name="android:windowBackground">@drawable/background</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:textColorHint">@android:color/black</item>
<item name="android:windowFullscreen">true</item>

</style>
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();

}

}
}
}

Comments

Post a Comment

Popular posts from this blog

 var val  deference in kotlin  fun main () { var Android_Programing_language : String = "java" println ( "android official programing language: " + Android_Programing_language ) Android_Programing_language = "Kotline" println ( "2019 google announced android official programing language: " + Android_Programing_language ) val Android_MultiPlatform_language : String = "Dart" println ( "multiple device run : " + Android_MultiPlatform_language ) /* Android_MultiPlatform_language="" /* note: var--------------valu changable note: val--------------valu not changable (final) */ */ }
 Firebase Cloud Messaging With  push notification with Image  public class ImageDownload extends AsyncTask < String , Void , Bitmap >{ @Override protected Bitmap doInBackground ( String ... strings ) { InputStream inputStream ; try { URL url = new URL( strings [ 0 ]) ; try { HttpURLConnection connection = ( HttpURLConnection ) url .openConnection() ; connection .connect() ; inputStream = connection .getInputStream() ; return BitmapFactory . decodeStream ( inputStream ) ; } catch ( IOException e ) { e .printStackTrace() ; } } catch ( MalformedURLException e ) { e .printStackTrace() ; } return null; } @Override protected void onPostExecute ( Bitmap bitmap ) { ShowNotification( bitmap ) ; } }
  Kotlin Function function means use big problems divided by submodule and easily problems solved.   It makes reusability of code and makes the program more manageable There are two types of functions: Standard library function User-defined function Standard Library Function Kotlin Standard library function is built-in library functions import java.time.LocalDateTime fun main (args: Array < String >) { val current = LocalDateTime.now() println( "Current Date and Time is: $current " ) } User-defined Function A user-defined is created by the user. User-defined function takes the parameter(s), perform an action and return the result of that action as a value. fun  main(args: Array<String>){       sum()       print( "code after sum" )   }   fun  sum(){       var num1 = 5        var...