0
0
Android Kotlinmobile~5 mins

Firebase Authentication in Android Kotlin

Choose your learning style9 modes available
Introduction

Firebase Authentication helps you easily add user sign-in to your app. It keeps user accounts safe and lets users log in quickly.

When you want users to create accounts and log in to your app.
When you need to keep user data private and secure.
When you want to let users sign in with email and password or social accounts like Google.
When you want to manage user sessions without building your own backend.
When you want to quickly add authentication without complex setup.
Syntax
Android Kotlin
val auth = FirebaseAuth.getInstance()
auth.createUserWithEmailAndPassword(email, password)
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            // User created
        } else {
            // Handle error
        }
    }

Use FirebaseAuth.getInstance() to get the authentication object.

Use createUserWithEmailAndPassword to register new users.

Examples
Sign in an existing user with email and password.
Android Kotlin
val auth = FirebaseAuth.getInstance()
auth.signInWithEmailAndPassword(email, password)
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            // User signed in
        } else {
            // Sign-in failed
        }
    }
Check if a user is currently signed in.
Android Kotlin
val user = FirebaseAuth.getInstance().currentUser
if (user != null) {
    // User is signed in
} else {
    // No user signed in
}
Sign out the current user.
Android Kotlin
FirebaseAuth.getInstance().signOut()
Sample App

This simple app lets users sign up with email and password. When the sign-up button is clicked, it tries to create a new user. If successful, it shows a success message; otherwise, it shows an error.

Android Kotlin
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 MainActivity : AppCompatActivity() {
    private lateinit var auth: FirebaseAuth

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

        auth = FirebaseAuth.getInstance()

        val emailInput = findViewById<EditText>(R.id.emailInput)
        val passwordInput = findViewById<EditText>(R.id.passwordInput)
        val signUpButton = findViewById<Button>(R.id.signUpButton)

        signUpButton.setOnClickListener {
            val email = emailInput.text.toString()
            val password = passwordInput.text.toString()

            auth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this) { task ->
                    if (task.isSuccessful) {
                        Toast.makeText(this, "Sign up successful!", Toast.LENGTH_SHORT).show()
                    } else {
                        Toast.makeText(this, "Sign up failed: ${task.exception?.message}", Toast.LENGTH_SHORT).show()
                    }
                }
        }
    }
}
OutputSuccess
Important Notes

Always validate email and password before sending to Firebase.

Firebase handles password security, so you don't store passwords yourself.

Use addOnCompleteListener to handle success or failure asynchronously.

Summary

Firebase Authentication makes user sign-in easy and secure.

Use createUserWithEmailAndPassword to register users.

Check current user with currentUser and sign out with signOut().