Firebase Authentication Android With Email and Password

Firebase Authentication Android provides backend services to developers out there, in order to authenticate users in the application.

In this tutorial, we will implement the Firebase Authentication Android service provided by Firebase using Email and Password SignIn method.

But first we need to connect our Android project with Firebase to start using Firebase services.

Connecting Firebase project to Android app through Android studio Firebase assistant

To connect Firebase through Android Studio assistant you need to sign in to Android studio with your email. To sign in, click the icon at the top right corner in Android studio and SignIn.

Steps to connect Firebase to Android app:

  1. Launch Firebase assistant by heading to Tools->Firebase.

2. Click on “Authentication” and select “Authenticate using a custom authentication system”.

3. Click on “Connect to Firebase”.

4. By clicking Connect to Firebase. You’ll be taken to the Firebase website to choose a Firebase project (Create new Firebase project if you don't have one).

Here we will select the existing one named FirebaseAuthentication.

5. Click on “Connect”.

Now you will see a message like the one below.

6. Go back Android Studio and click “Add Firebase Authentication to your app” and hit “Accept changes”, so this will add the required dependencies to your project.

7. Open your Firebase project again and go to Authentication section and select Email/Password to enable it.

8. Enable the first option and click save so it will enable the Email/Password signin method.

Congratulations! You have finished connecting and setting up the Firebase Authentication.

Application code:

Create new activity “RegisterActivity” and paste the below code in activity_register.xml.

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp"
tools:context=".RegisterActivity">

<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:src="@drawable/firebase_image"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_marginTop="25dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="@color/teal_700"
android:gravity="center_horizontal"
android:layout_marginVertical="16dp"/>


<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_height="wrap_content">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etRegEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"/>
</com.google.android.material.textfield.TextInputLayout>


<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_height="wrap_content"
android:layout_marginTop="12dp">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etRegPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/btnRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Register"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Already registered"
android:textSize="16sp"/>

<TextView
android:id="@+id/tvLoginHere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="Login here"
android:textColor="@color/teal_700"
android:textStyle="bold"
android:textSize="20sp"/>

</LinearLayout>

</LinearLayout>

</LinearLayout>

Now open RegisterActivity.java and paste the below code in your Java file.

public class RegisterActivity extends AppCompatActivity {    TextInputEditText etRegEmail;
TextInputEditText etRegPassword;
TextView tvLoginHere;
Button btnRegister;
FirebaseAuth mAuth; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
etRegEmail = findViewById(R.id.etRegEmail);
etRegPassword = findViewById(R.id.etRegPass);
tvLoginHere = findViewById(R.id.tvLoginHere);
btnRegister = findViewById(R.id.btnRegister);
mAuth = FirebaseAuth.getInstance(); btnRegister.setOnClickListener(view ->{
createUser();
});
tvLoginHere.setOnClickListener(view ->{
startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
});
}
private void createUser(){
String email = etRegEmail.getText().toString();
String password = etRegPassword.getText().toString();
if (TextUtils.isEmpty(email)){
etRegEmail.setError("Email cannot be empty");
etRegEmail.requestFocus();
}else if (TextUtils.isEmpty(password)){
etRegPassword.setError("Password cannot be empty");
etRegPassword.requestFocus();
}else{
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
Toast.makeText(RegisterActivity.this, "User registered successfully", Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
}else{
Toast.makeText(RegisterActivity.this, "Registration Error: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
}

Create new activity “LoginActivity” and paste below code to activity_login.xml.

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp"
tools:context=".LoginActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:src="@drawable/firebase_image"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_marginTop="25dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="@color/teal_700"
android:gravity="center_horizontal"
android:layout_marginVertical="16dp"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etLoginEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_height="wrap_content"
android:layout_marginTop="12dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etLoginPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Login"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not registered yet"
android:textSize="16sp"/>
<TextView
android:id="@+id/tvRegisterHere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="Register here"
android:textColor="@color/teal_700"
android:textStyle="bold"
android:textSize="20sp"/>
</LinearLayout> </LinearLayout></LinearLayout>

After this open your “LoginActivity.java” and paste the below code in your Java file.

public class LoginActivity extends AppCompatActivity {    TextInputEditText etLoginEmail;
TextInputEditText etLoginPassword;
TextView tvRegisterHere;
Button btnLogin;
FirebaseAuth mAuth; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etLoginEmail = findViewById(R.id.etLoginEmail);
etLoginPassword = findViewById(R.id.etLoginPass);
tvRegisterHere = findViewById(R.id.tvRegisterHere);
btnLogin = findViewById(R.id.btnLogin);
mAuth = FirebaseAuth.getInstance(); btnLogin.setOnClickListener(view -> {
loginUser();
});
tvRegisterHere.setOnClickListener(view ->{
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
});
}
private void loginUser(){
String email = etLoginEmail.getText().toString();
String password = etLoginPassword.getText().toString();
if (TextUtils.isEmpty(email)){
etLoginEmail.setError("Email cannot be empty");
etLoginEmail.requestFocus();
}else if (TextUtils.isEmpty(password)){
etLoginPassword.setError("Password cannot be empty");
etLoginPassword.requestFocus();
}else{
mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
Toast.makeText(LoginActivity.this, "User logged in successfully", Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
}else{
Toast.makeText(LoginActivity.this, "Log in Error: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
}

Now we have to add a logout button to our MainActivity so the user will have a logout option when logged in.

Open activity_main.xml and paste the following code.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome, You are logged in "
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Logout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>

After this we will implement logout option in our MainActivity.java, so paste the below code in your MainActivity.java.

public class MainActivity extends AppCompatActivity {    Button btnLogOut;
FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogOut = findViewById(R.id.btnLogout);
mAuth = FirebaseAuth.getInstance();
btnLogOut.setOnClickListener(view ->{
mAuth.signOut();
startActivity(new Intent(MainActivity.this, LoginActivity.class));
});
} @Override
protected void onStart() {
super.onStart();
FirebaseUser user = mAuth.getCurrentUser();
if (user == null){
startActivity(new Intent(MainActivity.this, LoginActivity.class));
}
}
}

Now the application is ready to use just run your application on emulator or a real device and try to register yourself by using your email id and password.

--

--

--

A Software Engineer with 3 years of experience in Software development. Visit me at https://about.me/hilalahmad

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Storing Data Locally With Room Database and MVVM Architecture

Old Flutter project not working since flutter upgrade? :(

Public testing of Android app starts now!

How to make weather app in Android Studio with Source code

Weather app design

How To Create Listview — android

Draw security pattern with Appium -Andriod

Building a Team Lineup View on Android

Unboxing Jetpack Compose

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hilal Ahmad

Hilal Ahmad

A Software Engineer with 3 years of experience in Software development. Visit me at https://about.me/hilalahmad

More from Medium

Top Reasons to make the Future of Android App Development with Flutter.

Setup Cordova for Android

Adding Flutter in Existing Android App

Create android emulator with command line