A free Firebase project (console.firebase.google.com)
An emulator or an Android phone
How it fits together
1MainActivity checks currentUser
2LoginActivity or RegisterActivity
3Firebase Authentication
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:
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.
Files affected
Check what to add and what to replace before copying code.
The home screen. Sends signed-out users to Login, and shows who is signed in.
MainActivity.java
packagecom.example.firebaselogin;importandroid.content.Intent;importandroid.os.Bundle;importandroid.widget.Button;importandroid.widget.TextView;importandroidx.appcompat.app.AppCompatActivity;importcom.google.firebase.auth.FirebaseAuth;importcom.google.firebase.auth.FirebaseUser;/** The home screen. Firebase remembers who is signed in, so returning users skip the login screen. */publicclassMainActivityextendsAppCompatActivity{privatefinalFirebaseAuth auth =FirebaseAuth.getInstance();@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);FirebaseUser user = auth.getCurrentUser();if(user ==null){startActivity(newIntent(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(newIntent(this,LoginActivity.class));finish();});}}