Skip to Content

Firebase Authentication in Android (Java) – Complete Guide

 

Introduction

Firebase Authentication is one of the easiest ways to add user authentication to your Android application. It allows users to create accounts, log in securely, reset passwords, and manage their sessions without building your own authentication backend.

In this tutorial, you will learn how to implement Email/Password Authentication in Android using Java and Firebase Authentication.

What You Will Learn

  • Create a Firebase project

  • Connect Android app with Firebase

  • User Registration

  • User Login

  • User Logout

  • Check User Session

  • Forgot Password


Prerequisites

Before starting, make sure you have:

  • Android Studio installed

  • Firebase account

  • Basic knowledge of Java

  • Internet connection


Step 1: Create Firebase Project

  1. Open Firebase Console

  2. Click Create Project

  3. Enter Project Name

  4. Click Continue

  5. Complete project setup


Step 2: Add Android App

  1. Click Add App

  2. Select Android

  3. Enter Package Name

Example:

com.palcoder.firebaseauth
  1. Register App

  2. Download google-services.json

  3. Place it inside the app folder


Step 3: Enable Authentication

  1. Open Firebase Console

  2. Go to Authentication

  3. Click Get Started

  4. Open Sign-in Method

  5. Enable Email/Password

  6. Save


Step 4: Add Firebase Dependencies

Project Level build.gradle

classpath 'com.google.gms:google-services:4.4.2'

App Level build.gradle

implementation 'com.google.firebase:firebase-auth:23.0.0'

Add at bottom:

apply plugin: 'com.google.gms.google-services'

Sync Project.


Step 5: Create Registration Layout

activity_register.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="20dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/etEmail"
        android:hint="Email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/etPassword"
        android:hint="Password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btnRegister"
        android:text="Register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

Step 6: Registration Code

RegisterActivity.java

package com.palcoder.firebaseauth;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.auth.FirebaseAuth;

public class RegisterActivity extends AppCompatActivity {

    EditText etEmail, etPassword;
    Button btnRegister;

    FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        etEmail = findViewById(R.id.etEmail);
        etPassword = findViewById(R.id.etPassword);
        btnRegister = findViewById(R.id.btnRegister);

        mAuth = FirebaseAuth.getInstance();

        btnRegister.setOnClickListener(v -> {

            String email = etEmail.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            if(TextUtils.isEmpty(email)){
                etEmail.setError("Enter Email");
                return;
            }

            if(TextUtils.isEmpty(password)){
                etPassword.setError("Enter Password");
                return;
            }

            mAuth.createUserWithEmailAndPassword(email,password)
                    .addOnCompleteListener(task -> {

                        if(task.isSuccessful()){
                            Toast.makeText(
                                    RegisterActivity.this,
                                    "Registration Successful",
                                    Toast.LENGTH_SHORT
                            ).show();
                        }
                        else{
                            Toast.makeText(
                                    RegisterActivity.this,
                                    task.getException().getMessage(),
                                    Toast.LENGTH_SHORT
                            ).show();
                        }

                    });

        });

    }
}

Step 7: Create Login Layout

activity_login.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="20dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/etEmail"
        android:hint="Email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/etPassword"
        android:hint="Password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btnLogin"
        android:text="Login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

Step 8: Login Code

LoginActivity.java

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.auth.FirebaseAuth;

public class LoginActivity extends AppCompatActivity {

    EditText etEmail, etPassword;
    Button btnLogin;

    FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        etEmail = findViewById(R.id.etEmail);
        etPassword = findViewById(R.id.etPassword);
        btnLogin = findViewById(R.id.btnLogin);

        mAuth = FirebaseAuth.getInstance();

        btnLogin.setOnClickListener(v -> {

            String email = etEmail.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            mAuth.signInWithEmailAndPassword(email,password)
                    .addOnCompleteListener(task -> {

                        if(task.isSuccessful()){

                            Toast.makeText(
                                    LoginActivity.this,
                                    "Login Successful",
                                    Toast.LENGTH_SHORT
                            ).show();

                        }else{

                            Toast.makeText(
                                    LoginActivity.this,
                                    task.getException().getMessage(),
                                    Toast.LENGTH_SHORT
                            ).show();

                        }

                    });

        });

    }
}

Step 9: Logout User

FirebaseAuth.getInstance().signOut();

Example:

btnLogout.setOnClickListener(v -> {

    FirebaseAuth.getInstance().signOut();

});

Step 10: Check User Session

if(FirebaseAuth.getInstance().getCurrentUser() != null){

    // User already logged in

}

Use this in Splash Screen or Login Activity.


Step 11: Forgot Password

FirebaseAuth.getInstance()
        .sendPasswordResetEmail(email)
        .addOnCompleteListener(task -> {

            if(task.isSuccessful()){

                Toast.makeText(
                        this,
                        "Reset Email Sent",
                        Toast.LENGTH_SHORT
                ).show();

            }

        });

Common Errors

Email Already Exists

The email address is already in use by another account.

Solution:

Use a different email.


Weak Password

Password should be at least 6 characters.

Solution:

Use a stronger password.


Invalid Email

The email address is badly formatted.

Solution:

Enter a valid email address.


Best Practices

  • Validate email before registration.

  • Use strong passwords.

  • Enable Email Verification.

  • Check internet connection.

  • Keep users logged in using Firebase Session.


Conclusion

Congratulations! You have successfully implemented Firebase Authentication in Android using Java. Users can now register, log in, reset passwords, and stay authenticated using Firebase Authentication.

Firebase Auth is widely used in Android applications because it is secure, scalable, and easy to integrate.

Next Tutorial

  • Firebase Realtime Database in Android (Java)

  • Firebase Firestore in Android

  • Firebase Storage Upload Image

  • Firebase Phone Authentication

  • Firebase Email Verification

Disclaimer: The information, code snippets, and tutorials presented on this website are for educational and informational purposes only. While we make every effort to provide accurate, tested, and high-quality contents, all materials are provided "as is" without warranty of any kind. Use them at your own discretion and risk.

Enjoyed this tutorial?

Consider supporting Pal Coder so we can keep creating high-quality, free Android development resources.