<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luv Dumka</title>
    <description>The latest articles on DEV Community by Luv Dumka (@luv_dumka_73b5c3e11dcea61).</description>
    <link>https://dev.to/luv_dumka_73b5c3e11dcea61</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2459186%2Fd3c77ab6-a50f-4f77-bf3c-7526c8cd4cc6.png</url>
      <title>DEV Community: Luv Dumka</title>
      <link>https://dev.to/luv_dumka_73b5c3e11dcea61</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/luv_dumka_73b5c3e11dcea61"/>
    <language>en</language>
    <item>
      <title>Firebase Authentication in Android using Kotlin (Login &amp; Register)</title>
      <dc:creator>Luv Dumka</dc:creator>
      <pubDate>Fri, 11 Apr 2025 17:54:38 +0000</pubDate>
      <link>https://dev.to/luv_dumka_73b5c3e11dcea61/firebase-authentication-in-android-using-kotlin-login-register-4njp</link>
      <guid>https://dev.to/luv_dumka_73b5c3e11dcea61/firebase-authentication-in-android-using-kotlin-login-register-4njp</guid>
      <description>&lt;p&gt;Firebase Authentication in Android using Kotlin (Login &amp;amp; Register)&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

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

&lt;p&gt;First, I created a new Firebase project:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then, I updated the Gradle files.&lt;/p&gt;

&lt;p&gt;In the project-level build.gradle file:&lt;/p&gt;

&lt;p&gt;classpath 'com.google.gms:google-services:4.3.15'&lt;/p&gt;

&lt;p&gt;In the app-level build.gradle file:&lt;/p&gt;

&lt;p&gt;plugins {&lt;br&gt;
    id 'com.android.application'&lt;br&gt;
    id 'com.google.gms.google-services'&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;dependencies {&lt;br&gt;
    implementation 'com.google.firebase:firebase-auth-ktx:22.1.1'&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;After syncing the project, Firebase was ready to go.&lt;br&gt;
Step 2: Design a Simple Login/Register Layout&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;activity_main.xml&lt;/p&gt;

&lt;p&gt;
    xmlns:android="http://schemas.android.com/apk/res/android"&lt;br&gt;
    android:layout_width="match_parent"&lt;br&gt;
    android:layout_height="match_parent"&lt;br&gt;
    android:orientation="vertical"&lt;br&gt;
    android:gravity="center"&lt;br&gt;
    android:padding="24dp"&amp;gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;EditText
    android:id="@+id/emailEditText"
    android:hint="Email"
    android:inputType="textEmailAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" /&amp;gt;

&amp;lt;EditText
    android:id="@+id/passwordEditText"
    android:hint="Password"
    android:inputType="textPassword"
    android:layout_marginTop="12dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" /&amp;gt;

&amp;lt;Button
    android:id="@+id/loginButton"
    android:text="Login"
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" /&amp;gt;

&amp;lt;Button
    android:id="@+id/registerButton"
    android:text="Register"
    android:layout_marginTop="12dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3: Add Firebase Login and Register Logic&lt;/p&gt;

&lt;p&gt;In MainActivity.kt, I added the logic to handle user login and registration.&lt;/p&gt;

&lt;p&gt;MainActivity.kt&lt;/p&gt;

&lt;p&gt;package com.example.firebaseauthdemo&lt;/p&gt;

&lt;p&gt;import android.os.Bundle&lt;br&gt;
import android.widget.*&lt;br&gt;
import androidx.appcompat.app.AppCompatActivity&lt;br&gt;
import com.google.firebase.auth.FirebaseAuth&lt;/p&gt;

&lt;p&gt;class MainActivity : AppCompatActivity() {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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 -&amp;gt;
            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 -&amp;gt;
            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()
            }
        }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Final Thoughts&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;This setup is great for getting started quickly. From here, you can explore more features like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Email verification

Password reset

Google Sign-In

Protecting routes after login

Saving user data with Firestore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

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

</description>
    </item>
  </channel>
</rss>
