Firebase Authentication in Android using Kotlin (Login & Register)

Firebase Authentication in Android using Kotlin (Login & Register) When building an Android app, one of the first things you might need is a user login system. Instead of building everything from scratch, I decided to use Firebase Authentication, which turned out to be fast, secure, and really simple to integrate. Here’s how I implemented a basic email/password login and register feature using Firebase in an Android project written in Kotlin. Step 1: Set Up Firebase in Android Studio First, I created a new Firebase project: Go to the Firebase Console. Click on "Add Project" and follow the steps (I skipped Google Analytics). Click on “Add App” and choose Android. Register your app’s package name. Download the google-services.json file and place it inside the app/ folder of your Android Studio project. Then, I updated the Gradle files. In the project-level build.gradle file: classpath 'com.google.gms:google-services:4.3.15' In the app-level build.gradle file: plugins { id 'com.android.application' id 'com.google.gms.google-services' } dependencies { implementation 'com.google.firebase:firebase-auth-ktx:22.1.1' } After syncing the project, Firebase was ready to go. Step 2: Design a Simple Login/Register Layout Here’s the UI layout I used. It includes two EditTexts for email and password, and two Buttons — one for login and another for registration. activity_main.xml xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" android:padding="24dp"> Step 3: Add Firebase Login and Register Logic In MainActivity.kt, I added the logic to handle user login and registration. MainActivity.kt package com.example.firebaseauthdemo import android.os.Bundle import android.widget.* import androidx.appcompat.app.AppCompatActivity import com.google.firebase.auth.FirebaseAuth class MainActivity : AppCompatActivity() { private lateinit var auth: FirebaseAuth private lateinit var emailEditText: EditText private lateinit var passwordEditText: EditText private lateinit var loginButton: Button private lateinit var registerButton: Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) auth = FirebaseAuth.getInstance() emailEditText = findViewById(R.id.emailEditText) passwordEditText = findViewById(R.id.passwordEditText) loginButton = findViewById(R.id.loginButton) registerButton = findViewById(R.id.registerButton) loginButton.setOnClickListener { loginUser() } registerButton.setOnClickListener { registerUser() } } private fun loginUser() { val email = emailEditText.text.toString().trim() val password = passwordEditText.text.toString().trim() if (email.isEmpty() || password.isEmpty()) { Toast.makeText(this, "Please enter both email and password", Toast.LENGTH_SHORT).show() return } auth.signInWithEmailAndPassword(email, password) .addOnCompleteListener(this) { task -> if (task.isSuccessful) { Toast.makeText(this, "Login successful", Toast.LENGTH_SHORT).show() } else { Toast.makeText(this, "Login failed: ${task.exception?.message}", Toast.LENGTH_LONG).show() } } } private fun registerUser() { val email = emailEditText.text.toString().trim() val password = passwordEditText.text.toString().trim() if (email.isEmpty() || password.isEmpty()) { Toast.makeText(this, "Please fill all fields", Toast.LENGTH_SHORT).show() return } auth.createUserWithEmailAndPassword(email, password) .addOnCompleteListener(this) { task -> if (task.isSuccessful) { Toast.makeText(this, "Account created successfully", Toast.LENGTH_SHORT).show() } else { Toast.makeText(this, "Registration failed: ${task.exception?.message}", Toast.LENGTH_LONG).show() } } } } Final Thoughts Using Firebase Authentication helped me avoid a lot of backend setup. With just a few lines of Kotlin code, I was able to create a secure login and registration system for my app. This setup is great for getting started quickly. From here, you can explore more features like: Email verification Password reset Google Sign-In Protecting routes after login Saving user data with Firestore This was a great hands-on experience, and I hope it helps others who are looking to add Firebase Auth in their apps.

Apr 11, 2025 - 19:18
 0
Firebase Authentication in Android using Kotlin (Login & Register)

Firebase Authentication in Android using Kotlin (Login & Register)

When building an Android app, one of the first things you might need is a user login system. Instead of building everything from scratch, I decided to use Firebase Authentication, which turned out to be fast, secure, and really simple to integrate.

Here’s how I implemented a basic email/password login and register feature using Firebase in an Android project written in Kotlin.
Step 1: Set Up Firebase in Android Studio

First, I created a new Firebase project:

Go to the Firebase Console.

Click on "Add Project" and follow the steps (I skipped Google Analytics).

Click on “Add App” and choose Android.

Register your app’s package name.

Download the google-services.json file and place it inside the app/ folder of your Android Studio project.

Then, I updated the Gradle files.

In the project-level build.gradle file:

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

In the app-level build.gradle file:

plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}

dependencies {
implementation 'com.google.firebase:firebase-auth-ktx:22.1.1'
}

After syncing the project, Firebase was ready to go.
Step 2: Design a Simple Login/Register Layout

Here’s the UI layout I used. It includes two EditTexts for email and password, and two Buttons — one for login and another for registration.

activity_main.xml

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="24dp">





Step 3: Add Firebase Login and Register Logic

In MainActivity.kt, I added the logic to handle user login and registration.

MainActivity.kt

package com.example.firebaseauthdemo

import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth

class MainActivity : AppCompatActivity() {

private lateinit var auth: FirebaseAuth
private lateinit var emailEditText: EditText
private lateinit var passwordEditText: EditText
private lateinit var loginButton: Button
private lateinit var registerButton: Button

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    auth = FirebaseAuth.getInstance()

    emailEditText = findViewById(R.id.emailEditText)
    passwordEditText = findViewById(R.id.passwordEditText)
    loginButton = findViewById(R.id.loginButton)
    registerButton = findViewById(R.id.registerButton)

    loginButton.setOnClickListener { loginUser() }
    registerButton.setOnClickListener { registerUser() }
}

private fun loginUser() {
    val email = emailEditText.text.toString().trim()
    val password = passwordEditText.text.toString().trim()

    if (email.isEmpty() || password.isEmpty()) {
        Toast.makeText(this, "Please enter both email and password", Toast.LENGTH_SHORT).show()
        return
    }

    auth.signInWithEmailAndPassword(email, password)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                Toast.makeText(this, "Login successful", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "Login failed: ${task.exception?.message}", Toast.LENGTH_LONG).show()
            }
        }
}

private fun registerUser() {
    val email = emailEditText.text.toString().trim()
    val password = passwordEditText.text.toString().trim()

    if (email.isEmpty() || password.isEmpty()) {
        Toast.makeText(this, "Please fill all fields", Toast.LENGTH_SHORT).show()
        return
    }

    auth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                Toast.makeText(this, "Account created successfully", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "Registration failed: ${task.exception?.message}", Toast.LENGTH_LONG).show()
            }
        }
}

}

Final Thoughts

Using Firebase Authentication helped me avoid a lot of backend setup. With just a few lines of Kotlin code, I was able to create a secure login and registration system for my app.

This setup is great for getting started quickly. From here, you can explore more features like:

Email verification

Password reset

Google Sign-In

Protecting routes after login

Saving user data with Firestore

This was a great hands-on experience, and I hope it helps others who are looking to add Firebase Auth in their apps.