In this article, we will learn about Authentication- integrating Email authentication in Android application using firebase. If you want to integrate Email and password authentication into your Android app, you’ve come to the right place.
We’ll use the Email and Password SignIn method to implement the Firebase Authentication Android service provided by Firebase. Authentication with Firebase Backend services is provided to Android developers in order to authenticate users in their apps.
The technology used to set up service-access permissions is called Firebase authentication. It accomplishes this by utilizing Google’s Firebase APIs and Firebase console to create and manage valid user accounts. You’ll learn how to use Firebase authentication in an Android application in this tutorial.
Output:
If you don’t know what is firebase and its services then please refer to What is Firebase | All about Firebase.
Google SignIn Using Firebase in Android | Authentication
Step by step explanation:
Step 1: Go to the Main Menu and select “Tools” and after that select “Firebase”.
Step 2: Select Authentication as shown in the below figure.
Step 3: Select the option- “Authentication using custom authentication system Kotlin”.
Step 4: Connect your app to firebase: First.
- After that Add the Firebase Authentication SDK to your app as shown in figure given below:
Step 5: Go to Firebase console: Enable Email/Password Authentication as shown below:
Step 6: Enable it, Click save button as shown below.
dependency required in Google Signin
Step 8: Add this to your file build.gradle(Module:app):
//Firebase implementation platform(‘com.google.firebase:firebase-bom:28.4.0’) implementation ‘com.google.firebase:firebase-auth-ktx’ implementation ‘com.google.android.gms:play-services-auth:19.2.0’ |
Plugin required in Google Signin
Add this to your file build.gradle(Module:app):
id ‘com.google.gms.google-services’ |
Add dependencies in build.gradle(Project:app)
classpath ‘com.google.gms:google-services:4.3.10’ |
Finally, your build.gradle(Module:app) file code look like this:
plugins { id 'com.android.application' id 'kotlin-android' id 'com.google.gms.google-services' } android { compileSdkVersion 30 buildToolsVersion "30.0.3" defaultConfig { applicationId "com.sagar.emailauthentication" 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 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.6.0' implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.0' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' //Firebase implementation platform('com.google.firebase:firebase-bom:28.4.0') implementation 'com.google.firebase:firebase-auth-ktx' implementation 'com.google.android.gms:play-services-auth:19.2.0' }
and build.gradle(Project:app) code look like this:
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = "1.5.10" repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:4.2.2" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.3.10' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() mavenCentral() jcenter() // Warning: this repository is going to shut down soon } } task clean(type: Delete) { delete rootProject.buildDir }
Step 9: Your AndroidManifest.xml file is look like:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sagar.emailauthentication"> <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.EmailAuthentication"> <activity android:name=".LoginActivity"></activity> <activity android:name=".RegisterActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity"> </activity> </application> </manifest>
Step 10: Create a new activity(Empty Activity ), name it RegisterActivity, and add the code given below:
import android.content.Intent import android.os.Bundle import android.text.TextUtils import android.view.Window import android.view.WindowManager import android.widget.Button import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.android.material.textfield.TextInputEditText import com.google.firebase.auth.FirebaseAuth class RegisterActivity : AppCompatActivity() { var etRegEmail: TextInputEditText? = null var etRegPassword: TextInputEditText? = null var tvLoginHere: TextView? = null var btnRegister: Button? = null var mAuth: FirebaseAuth? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_register) etRegEmail = findViewById(R.id.etRegEmail) etRegPassword = findViewById(R.id.etRegPass) tvLoginHere = findViewById(R.id.tvLoginHere) btnRegister = findViewById(R.id.btnRegister) mAuth = FirebaseAuth.getInstance() findViewById<Button>(R.id.btnRegister).setOnClickListener { createUser() } findViewById<TextView>(R.id.tvLoginHere).setOnClickListener { startActivity( Intent( this@RegisterActivity, LoginActivity::class.java ) ) } } private fun createUser() { val email = etRegEmail!!.text.toString() val password = etRegPassword!!.text.toString() if (TextUtils.isEmpty(email)) { etRegEmail!!.error = "Email cannot be empty" etRegEmail!!.requestFocus() } else if (TextUtils.isEmpty(password)) { etRegPassword!!.error = "Password cannot be empty" etRegPassword!!.requestFocus() } else { mAuth!!.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task -> if (task.isSuccessful) { Toast.makeText( this@RegisterActivity, "User registered successfully", Toast.LENGTH_SHORT ).show() startActivity(Intent(this@RegisterActivity, MainActivity::class.java)) } else { Toast.makeText( this@RegisterActivity, "Registration Error: " + task.exception?.message, Toast.LENGTH_SHORT ).show() } } } } }
Step 11: Design a layout for activity_register.xml, add the code given below:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="24dp" tools:context=".RegisterActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_gravity="center" android:layout_marginTop="25dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Register" android:textSize="30sp" android:textStyle="bold" android:textColor="@color/teal_700" android:gravity="center_horizontal" android:layout_marginVertical="16dp"/> <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/etRegEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email" android:inputType="textEmailAddress"/> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_height="wrap_content" android:layout_marginTop="12dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/etRegPass" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword"/> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.button.MaterialButton android:id="@+id/btnRegister" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="Register"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Already registered" android:textSize="16sp"/> <TextView android:id="@+id/tvLoginHere" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:text="Login here" android:textColor="@color/teal_700" android:textStyle="bold" android:textSize="20sp"/> </LinearLayout> </LinearLayout> </LinearLayout>
Step 12: Create another activity(Empty Activity), name it LoginActivity, and add the code given below:
import android.content.Intent import android.os.Bundle import android.text.TextUtils import android.view.Window import android.view.WindowManager import android.widget.Button import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.android.material.textfield.TextInputEditText import com.google.firebase.auth.FirebaseAuth class LoginActivity : AppCompatActivity() { var etLoginEmail: TextInputEditText? = null var etLoginPassword: TextInputEditText? = null var tvRegisterHere: TextView? = null var btnLogin: Button? = null var mAuth: FirebaseAuth? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_login) etLoginEmail = findViewById(R.id.etLoginEmail) etLoginPassword = findViewById(R.id.etLoginPass) tvRegisterHere = findViewById(R.id.tvRegisterHere) btnLogin = findViewById(R.id.btnLogin) mAuth = FirebaseAuth.getInstance() findViewById<Button>(R.id.btnLogin).setOnClickListener { loginUser() } findViewById<TextView>(R.id.tvRegisterHere).setOnClickListener { startActivity( Intent( this@LoginActivity, RegisterActivity::class.java ) ) } } private fun loginUser() { val email = etLoginEmail!!.text.toString() val password = etLoginPassword!!.text.toString() if (TextUtils.isEmpty(email)) { etLoginEmail!!.error = "Email cannot be empty" etLoginEmail!!.requestFocus() } else if (TextUtils.isEmpty(password)) { etLoginPassword!!.error = "Password cannot be empty" etLoginPassword!!.requestFocus() } else { mAuth!!.signInWithEmailAndPassword(email, password).addOnCompleteListener { task -> if (task.isSuccessful) { Toast.makeText( this@LoginActivity, "User logged in successfully", Toast.LENGTH_SHORT ).show() startActivity(Intent(this@LoginActivity, MainActivity::class.java)) } else { Toast.makeText( this@LoginActivity, "Log in Error: " + task.exception?.message, Toast.LENGTH_SHORT ).show() } } } } }
Step 13: Design a layout for activity_login.xml, add the code given below:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="24dp" tools:context=".LoginActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginTop="25dp" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginVertical="16dp" android:gravity="center_horizontal" android:text="Login" android:textColor="@color/teal_700" android:textSize="30sp" android:textStyle="bold" /> <com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/etLoginEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email" android:inputType="textEmailAddress" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/etLoginPass" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.button.MaterialButton android:id="@+id/btnLogin" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="Login" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Not registered yet" android:textSize="16sp" /> <TextView android:id="@+id/tvRegisterHere" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:text="Register here" android:textColor="@color/teal_700" android:textSize="20sp" android:textStyle="bold" /> </LinearLayout> </LinearLayout> </LinearLayout>
Step 14: In MainActivity.kt file add the code given below:
import android.content.Intent import android.os.Bundle import android.view.Window import android.view.WindowManager import android.widget.Button import androidx.appcompat.app.AppCompatActivity import com.google.firebase.auth.FirebaseAuth class MainActivity : AppCompatActivity() { var btnLogOut: Button? = null var mAuth: FirebaseAuth? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main) btnLogOut = findViewById(R.id.btnLogout) mAuth = FirebaseAuth.getInstance() findViewById<Button>(R.id.btnLogout).setOnClickListener { view -> mAuth!!.signOut() startActivity(Intent(this@MainActivity, LoginActivity::class.java)) } } override fun onStart() { super.onStart() val user = mAuth!!.currentUser if (user == null) { startActivity(Intent(this@MainActivity, LoginActivity::class.java)) } } }
Step 15: In activity_main.xml add the code given below:
<?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=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="Congratulation- You Successfully Created Email Authentication " android:textAlignment="center" android:textSize="25sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.241" /> <Button android:id="@+id/btnLogout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="136dp" android:text="Logout" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
Run the application now. You will get the output shown in the output section.
For video reference:
email authentication firebase, email authentication firebase
email authentication, email authentication, email authentication.
Source Code:
We’ve completed the development of Email Authentication using Firebase in the Android application. You can get the Source code from the given below button.
- We hope that this guide will assist you in understanding all about the concepts of Email authentication using firebase in android application. We have concentrated on making a basic, meaningful and easy-to -learn guide to the concepts of Email authentication using firebase with suitable examples. Still if you have any problems regarding this, please post them in the comments section, we will be glad to assist you.
Pingback: Transparent Activity in Android studio - Developers Dome