Codementor Events

Login and Registration using Firebase in Android (Kotlin)

Published Mar 28, 2025
Login and Registration using Firebase in Android (Kotlin)

In this tutorial, we will learn how to implement login and registration in an Android application using Firebase Authentication. Firebase provides a simple way to authenticate users using various sign-in methods, including email and password authentication.

Prerequisites

  • Android Studio installed
  • A Firebase account
  • Basic knowledge of Kotlin and Android development

Step 1: Setting Up Firebase

  1. Go to Firebase Console
  2. Click on Get Started and create a new Firebase project.
  3. Name your project and click Continue.
  4. Disable Google Analytics for simplicity and click Create Project.
  5. Once the project is created, click Continue.
  6. In the Firebase Console, go to Build > Authentication and click Get Started.
  7. Enable Email/Password Authentication and click Save.

Step 2: Adding Firebase to Android Project

  1. Open Android Studio and create a new project.
  2. Navigate to Tools > Firebase.
  3. Select Authentication and click Connect to Firebase.
  4. Select the Firebase project you created earlier.
  5. Add Firebase Authentication SDK to your app by following on-screen instructions.
  6. Open build.gradle (Project-level) and verify that google-services is added:
    classpath 'com.google.gms:google-services:4.3.14'
    
  7. Open build.gradle (App-level) and ensure Firebase dependencies are added:
    implementation 'com.google.firebase:firebase-auth:21.0.1'
    
  8. Sync the project with Gradle.

Step 3: Designing Login and Registration UI

Login Layout (activity_login.xml)

<LinearLayout
    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">

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email"/>

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:inputType="textPassword"/>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"/>
</LinearLayout>

Registration Layout (activity_register.xml)

<LinearLayout
    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">

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email"/>

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:inputType="textPassword"/>

    <Button
        android:id="@+id/btn_register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register"/>
</LinearLayout>

Step 4: Implementing Registration

RegisterActivity.kt

class RegisterActivity : AppCompatActivity() {
    private lateinit var auth: FirebaseAuth
    private lateinit var email: EditText
    private lateinit var password: EditText
    private lateinit var registerBtn: Button

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

        auth = FirebaseAuth.getInstance()
        email = findViewById(R.id.email)
        password = findViewById(R.id.password)
        registerBtn = findViewById(R.id.btn_register)

        registerBtn.setOnClickListener {
            val emailText = email.text.toString()
            val passwordText = password.text.toString()

            if (emailText.isNotEmpty() && passwordText.isNotEmpty()) {
                registerUser(emailText, passwordText)
            } else {
                Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show()
            }
        }
    }

    private fun registerUser(email: String, password: String) {
        auth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    Toast.makeText(this, "Registration Successful", Toast.LENGTH_SHORT).show()
                    startActivity(Intent(this, LoginActivity::class.java))
                    finish()
                } else {
                    Toast.makeText(this, "Registration Failed", Toast.LENGTH_SHORT).show()
                }
            }
    }
}

Step 5: Implementing Login

LoginActivity.kt

class LoginActivity : AppCompatActivity() {
    private lateinit var auth: FirebaseAuth
    private lateinit var email: EditText
    private lateinit var password: EditText
    private lateinit var loginBtn: Button

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

        auth = FirebaseAuth.getInstance()
        email = findViewById(R.id.email)
        password = findViewById(R.id.password)
        loginBtn = findViewById(R.id.btn_login)

        loginBtn.setOnClickListener {
            val emailText = email.text.toString()
            val passwordText = password.text.toString()

            if (emailText.isNotEmpty() && passwordText.isNotEmpty()) {
                loginUser(emailText, passwordText)
            } else {
                Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show()
            }
        }
    }

    private fun loginUser(email: String, password: String) {
        auth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    Toast.makeText(this, "Login Successful", Toast.LENGTH_SHORT).show()
                    startActivity(Intent(this, MainActivity::class.java))
                    finish()
                } else {
                    Toast.makeText(this, "Login Failed", Toast.LENGTH_SHORT).show()
                }
            }
    }
}

By following these steps, you have successfully implemented Firebase authentication for login and registration in an Android app using Kotlin. You can further enhance security by implementing email verification and password reset features.

Discover and read more posts from Prashant Gosai
get started