Intermediate 35 min Java Full project

Login and Registration with Firebase Authentication (Java)

Build login and registration with email and password using Firebase Authentication. Accounts live in the cloud, so users can sign in from any device.

What you'll learn

  • Turn on Email/Password sign-in in Firebase
  • Register new users
  • Sign users in and out
  • Check who is signed in when the app opens

Before you start

  • Android Studio (any recent version)
  • Basic Java: variables, methods and if/else
  • A free Firebase project (console.firebase.google.com)
  • An emulator or an Android phone

How it fits together

  1. 1MainActivity checks currentUser
  2. 2LoginActivity or RegisterActivity
  3. 3Firebase Authentication
  4. 4Back to MainActivity

Build it step by step

1. Create a Firebase project

In the Firebase console, create a project and add an Android app with your package name (com.example.firebaselogin to copy the code unchanged). Download google-services.json and put it in your project's app folder.

2. Turn on email sign-in

In the console, open Authentication → Sign-in method and enable Email/Password.

3. Add Firebase to the app

In app/build.gradle.kts, add the plugin and dependencies:

kotlin
plugins {
    id("com.google.gms.google-services")
}

dependencies {
    implementation(platform("com.google.firebase:firebase-bom:35.1.0"))
    implementation("com.google.firebase:firebase-auth")
}

In the project-level build.gradle.kts, add:

kotlin
plugins {
    id("com.google.gms.google-services") version "4.5.0" apply false
}

Then press Sync Now. The Firebase BoM picks matching versions for you.

4. Decide where to start

MainActivity is the launcher. In onCreate, check FirebaseAuth.getInstance().getCurrentUser(). If it's null, open LoginActivity and finish. Otherwise show the user's email. Firebase remembers sign-ins, so returning users go straight in.

5. Register

RegisterActivity checks the email format, a minimum of 6 characters, and that both passwords match, then calls createUserWithEmailAndPassword. It handles the two common failures with clear messages: email already used and password too weak.

6. Log in

LoginActivity calls signInWithEmailAndPassword. On success, open MainActivity and clear the back stack, so Back doesn't return to the login screen.

7. Forgot password

Forgot password? calls sendPasswordResetEmail. Firebase emails a reset link; you don't write any server code.

8. Log out

auth.signOut(), then open LoginActivity.

9. Run it

Create an account, log out, log back in, then try Forgot password? with your real email. New users appear in the console under Authentication → Users.

If something goes wrong

  • Build error about google-services.json: the file must be in the app folder, and its package name must match applicationId.
  • "This operation is not allowed": turn on Email/Password in the console.
  • Nothing happens on login: check the device has internet.

What's improved over the video

Returning users skip the login screen, there's a working password reset, errors appear under the right field, and the back stack is handled properly.

Required setup

These are additions to an existing project—not replacement project files.

Add Firebase Authentication

Add these lines

Where: app/build.gradle.kts and build.gradle.kts

Add the plugin and dependencies, put google-services.json from the Firebase console in the app folder, then Sync Now.

app/build.gradle.kts and build.gradle.kts
// app/build.gradle.kts
plugins {
    id("com.google.gms.google-services")
}

dependencies {
    implementation(platform("com.google.firebase:firebase-bom:35.1.0"))
    implementation("com.google.firebase:firebase-auth")
}

// project-level build.gradle.kts
plugins {
    id("com.google.gms.google-services") version "4.5.0" apply false
}

Application code

Select a file to see its code and where it belongs.

This code uses package com.example.firebaselogin. Change that first line to your own package, the one at the top of your MainActivity.

Replace fileapp/src/main/java/com/example/firebaselogin/MainActivity.java

The home screen. Sends signed-out users to Login, and shows who is signed in.

MainActivity.java
package com.example.firebaselogin;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

/** The home screen. Firebase remembers who is signed in, so returning users skip the login screen. */
public class MainActivity extends AppCompatActivity {
    private final FirebaseAuth auth = FirebaseAuth.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FirebaseUser user = auth.getCurrentUser();
        if (user == null) {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
            return;
        }
        Screens.setUp(this, R.layout.activity_main);

        TextView email = findViewById(R.id.textEmail);
        email.setText(user.getEmail());

        Button logout = findViewById(R.id.buttonLogout);
        logout.setOnClickListener(v -> {
            auth.signOut();
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        });
    }
}

Full source code