Open In App

How to Setup Firebase Email Authentication in Android with Example

Last Updated : 21 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Service-access permissions are configured using a mechanism called Firebase authentication. This is accomplished by using Google’s Firebase APIs and Firebase console to create and manage legitimate user accounts. You’ll discover how to integrate Firebase authentication into an Android application in this article.

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.

To do this, launch Android Studio and Create an Empty Activity Project with the following configurations:

  • Name: Geeks for Geeks Auth.
  • Package name: as com.<your_name>.firebaseauth (without spaces).
  • Language: Kotlin.
  • Minimum SDK: API 24: Android 7.0
Firebase Email Authentication in Android

 

Step 2: Signing in to Android Studio

Find the login button in the right corner, and then tap login to log in to your firebase account.

 

Step 3: Creating a Firebase Project

A Firebase project is required, which we will later link to our Android app. We will have access to the majority of Firebase services through this project, including authentication and analytics.

There are various steps in this process:

  1. Launch Firebase, log in using your Google account, and then click the button in the top right corner to get to Firebase Console.
  2. Create a new firebase project, with the name of the project, like the one below:

Image #3: Creating a new project.

        3. Enable the analytics:

Image #4: Enabling the analytics.

Step 4: Connecting Firebase with the Application

A Firebase project can be linked to an Android app in two different methods.

  1. Adding the services configuration file by hand
    You can accomplish this by downloading the google-services.json file, inserting it into the root of the Android project, and manually adding the required dependencies.
  2. You need a running app for this process so that it can communicate with the firebase servers. This shows that the dependencies and project configuration files are functioning as intended. 
  3. Using the Firebase Assistant in Android Studio
    This approach takes care of the majority of the work for you and is simpler and more direct. We’ll employ this approach because it is straightforward.
  4. You can then follow this guide to connect firebase to your app.

Step 5: Adding the Firebase Dependencies

Image #5: Adding the Dependencies

classpath 'com.google.gms:google-services:4.3.13'  // Google Services plugin

apply plugin: 'com.google.gms.google-services'  // Google Services plugin

// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:30.3.1')

There is one additional Firebase-related detail. We need to enable email and password authentication in our project’s console because we wish to use it. 

Go to Authentication > Sign-in method and activate email and password authentication to accomplish that. This is how it ought to seem.

Image #6: Enabling authentication on the project.

Congratulations!

The configuration of Firebase authentication is complete.

Step 6: Adding the Code Sauce

We will now develop the user interface (UI) and backend for our application. Here’s where Kotlin and XML come into play. The firebase APIs will handle the server-side backend for us, so we don’t need to code anything. Isn’t that fantastic?

We need to keep our app organized first. The term “application architecture” refers to this. The three new packages to be added are extensions, utils, and views. Expand the java directory, right-click on the package name directory, and choose to add.

As we go forward, we’ll find out more about them.

Step 7: Working with activity_main.xml

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:chainUseRtl="true"
    tools:context=".GfGMainActivity"
    tools:ignore="Autofill">
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/create_account"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/userMail"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
    <EditText
        android:id="@+id/etEmail"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="@string/email"
        android:inputType="textEmailAddress"
        android:padding="10dp"
        app:layout_constraintBottom_toTopOf="@id/userPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />
  
    <EditText
        android:id="@+id/etPassword"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:hint="@string/password"
        android:inputType="textPassword"
        android:maxLength="8"
        android:padding="10dp"
        app:layout_constraintBottom_toTopOf="@id/passwordConfirmation"
        app:layout_constraintEnd_toEndOf="@id/etEmail"
        app:layout_constraintStart_toStartOf="@id/etEmail"
        app:layout_constraintTop_toBottomOf="@id/etEmail" />
  
    <EditText
        android:id="@+id/etConfirmPassword"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:hint="@string/confirm_password"
        android:inputType="textPassword"
        android:maxLength="8"
        android:padding="10dp"
        app:layout_constraintBottom_toTopOf="@id/passwordButton"
        app:layout_constraintEnd_toEndOf="@id/etPassword"
        app:layout_constraintStart_toStartOf="@id/etPassword"
        app:layout_constraintTop_toBottomOf="@id/etPassword" />
</androidx.constraintlayout.widget.ConstraintLayout>




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads