Firebase is a platform that helps developers build mobile and web apps. It provides useful tools like databases, cloud storage, and hosting. One of its main features is email and password login, so developers don’t have to create their own system for user authentication. This makes app development easier, faster, and more secure.
Step by Step Implementation:
Step 1: Create a new project and Connect to Firebase app
Refer to Adding firebase to android app and follow the steps to connect firebase to your project.
Step 2: Enable Email/Password Authentication in Firebase Console
Go to Firebase console and navigate to your project, and under Project Overview, scroll down.
Select Authentication and Click on Get Started on the next screen.

Select Email/Password under Sign-in method > Native providers.

Enable Email/Password sign-in and click on Save.
Step 3: Add Firebase Authentication SDK to your project
Go to Tools > Firebase which should open the Firebase Assistant tab. Then Select Authentication > Authentication using Google. On the next screen, select Add the Firebase Authentication SDK to your app. Select Accept Changes on the dialog box that appears next. This will add the necessary dependencies of Firebase Authentication to your app.

Step 4: Working with RegisterActivity and it's layout
Create a new activity by right clicking on app folder, then New > Activity> Empty views activity. Then add the following code to the RegisterActivity file in Java or Koltin and the xml code to the activity_register.xml file.
package org.geeksforgeeks.demo;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.AuthResult;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
public class RegisterActivity extends AppCompatActivity {
private EditText emailTextView, passwordTextView;
private Button button;
private FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
// Initialize FirebaseAuth instance
auth = FirebaseAuth.getInstance();
emailTextView = findViewById(R.id.email_edittext);
passwordTextView = findViewById(R.id.password_edittext);
button = findViewById(R.id.register_button);
button.setOnClickListener(v -> registerNewUser());
}
private void registerNewUser() {
// Get values from input fields
String email = emailTextView.getText().toString().trim();
String password = passwordTextView.getText().toString().trim();
// Validate input
if (email.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "Please enter credentials", Toast.LENGTH_LONG).show();
return;
}
// Register new user with Firebase
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, "Registration successful!", Toast.LENGTH_LONG).show();
// Navigate to MainActivity
startActivity(new Intent(RegisterActivity.this, MainActivity.class));
finish();
} else {
// Registration failed
Toast.makeText(RegisterActivity.this, "Registration failed! Please try again later", Toast.LENGTH_LONG).show();
}
}
});
}
}
package org.geeksforgeeks.demo
import android.content.Intent
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
class RegisterActivity : AppCompatActivity() {
private lateinit var emailTextView: EditText
private lateinit var passwordTextView: EditText
private lateinit var button: Button
private lateinit var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
// creating FirebaseAuth instance
auth = FirebaseAuth.getInstance()
emailTextView = findViewById(R.id.email_edittext)
passwordTextView = findViewById(R.id.password_edittext)
button = findViewById(R.id.register_button)
button.setOnClickListener {
registerNewUser()
}
}
private fun registerNewUser() {
// Take the value of two edit texts in Strings
val email = emailTextView.text.toString()
val password = passwordTextView.text.toString()
// Validations for input email and password
if (email.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "please enter credentials", Toast.LENGTH_LONG).show()
} else {
// create new user or register new user
auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task ->
if (task.isSuccessful) {
Toast.makeText(this, "Registration successful!", Toast.LENGTH_LONG).show()
// if the user created intent to login activity
startActivity(Intent(this, MainActivity::class.java))
} else {
// Registration failed
Toast.makeText(this, "Registration failed!!" + " Please try again later", Toast.LENGTH_LONG).show()
}
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="32dp"
android:orientation="vertical"
tools:context=".RegisterActivity">
<!-- TextView for heading -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:textSize="32sp"
android:layout_margin="32dp"
android:textStyle="bold"
/>
<!-- Edit text for email -->
<EditText
android:id="@+id/email_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your Email" />
<!-- Edit text for password -->
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your Password"
android:inputType="textPassword" />
<!-- Button for register with text "Register" -->
<Button
android:id="@+id/register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register" />
</LinearLayout>
Design UI:

Step 5: Working with LoginActivity and it's layout
Create a new activity by right clicking on app folder, then New > Activity> Empty views activity. Then add the following code to the LoginActivity file in Java or Koltin and the xml code to the activity_login.xml file.
package org.geeksforgeeks.demo;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.AuthResult;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
public class LoginActivity extends AppCompatActivity {
private EditText emailTextView, passwordTextView;
private Button button;
private FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Initialize FirebaseAuth instance
auth = FirebaseAuth.getInstance();
emailTextView = findViewById(R.id.email_edittext);
passwordTextView = findViewById(R.id.password_edittext);
button = findViewById(R.id.login_button);
button.setOnClickListener(v -> loginUserAccount());
}
private void loginUserAccount() {
String email = emailTextView.getText().toString().trim();
String password = passwordTextView.getText().toString().trim();
// Validate input
if (email.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "Please enter credentials", Toast.LENGTH_LONG).show();
return;
}
// Login existing user
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Login successful
Toast.makeText(LoginActivity.this, "Login successful!!", Toast.LENGTH_LONG).show();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
} else {
// Login failed
Toast.makeText(LoginActivity.this, "Login failed!!", Toast.LENGTH_LONG).show();
}
}
});
}
}
package org.geeksforgeeks.demo
import android.content.Intent
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
class LoginActivity : AppCompatActivity() {
private lateinit var emailTextView: EditText
private lateinit var passwordTextView: EditText
private lateinit var button: Button
private lateinit var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
// creating instance of FirebaseAuth
auth = FirebaseAuth.getInstance()
emailTextView = findViewById(R.id.email_edittext)
passwordTextView = findViewById(R.id.password_edittext)
button = findViewById(R.id.login_button)
button.setOnClickListener {
loginUserAccount()
}
}
private fun loginUserAccount() {
val email = emailTextView.text.toString()
val password = passwordTextView.text.toString()
// Validations for input email and password
if (email.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "please enter credentials", Toast.LENGTH_LONG).show()
} else {
// login existing user
auth.signInWithEmailAndPassword(email, password).addOnCompleteListener { task ->
if (task.isSuccessful) {
// login successful
Toast.makeText(this, "Login successful!!", Toast.LENGTH_LONG).show()
startActivity(Intent(this, MainActivity::class.java))
} else {
// login failed
Toast.makeText(this, "Login failed!!", Toast.LENGTH_LONG).show()
}
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="32dp"
android:orientation="vertical"
tools:context=".LoginActivity">
<!-- TextView for heading -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="32sp"
android:layout_margin="32dp"
android:textStyle="bold"
/>
<!-- Edit text for email -->
<EditText
android:id="@+id/email_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your Email" />
<!-- Edit text for password -->
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your Password"
android:inputType="textPassword" />
<!-- Button for register with text "Register" -->
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
Design UI:

Step 6: Working with MainActivity and it's layout
Navigate to app > java > package name > MainActivity and app > res > layout > activity_main.xml. Then add the following code to the MainActivity file in Java or Koltin and the xml code to the activity_main.xml file.
package org.geeksforgeeks.demo;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button register = findViewById(R.id.register);
Button login = findViewById(R.id.login);
register.setOnClickListener(v ->
startActivity(new Intent(MainActivity.this, RegisterActivity.class))
);
login.setOnClickListener(v ->
startActivity(new Intent(MainActivity.this, LoginActivity.class))
);
}
}
package org.geeksforgeeks.demo
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val register: Button = findViewById(R.id.register)
val login: Button = findViewById(R.id.login)
register.setOnClickListener {
startActivity(Intent(this, RegisterActivity::class.java))
}
login.setOnClickListener {
startActivity(Intent(this, LoginActivity::class.java))
}
}
}
<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:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Register" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Login" />
</LinearLayout>
Design UI:

Output:
Note: If you face any unknown compilation error while running the app, consider downgrading the Firebase auth dependency in build.gradle.kts from 23.2.0 to 21.0.1
A new user will be added to Users section in Authentication.
