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

 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 ) ; } }
In this tutorial, I will show how can I save String Android Internal Storage .txt file  Nested Class: 1.File file 2.FileOutputStream fileOutputStream 3.OutputStreamWriter outputStreamWriter Step 1: Android Manifest.xml Permission Add <? xml version ="1.0" encoding ="utf-8" ?> <manifest xmlns: android ="http://schemas.android.com/apk/res/android" package ="com.ruhul.string_save_txt_format" > <uses-permission android :name ="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission> <application android :allowBackup ="true" android :icon ="@mipmap/ic_launcher" android :label ="@string/app_name" android :roundIcon ="@mipmap/ic_launcher_round" android :supportsRtl ="true" android :theme ="@style/Theme.String_save_txt_format" > <activity android :name =".MainActivity" &