Logo

dev-resources.site

for different kinds of informations.

Authentication in Android Project with Firebase.

Published at
1/13/2025
Categories
kotlin
android
firebase
mobile
Author
harsh_lade
Categories
4 categories in total
kotlin
open
android
open
firebase
open
mobile
open
Author
10 person written this
harsh_lade
open
Authentication in Android Project with Firebase.

Introduction

We discussed the benefits and implementation of Firebase in our Android Studio Project. In this article, we will see the practical use of Firebase, we will learn how to use Authentication for your app using Firebase.

Prerequisites

  • Firebase has already been integrated in project.
  • google-service.json file has been added in your project.

Implementation

###1. Add Firebase authentication dependency in your build.gradle:

dependencies{
    //other dependencies...
    implementaion("com.google.firebase:firebase-auth:22.1.0")
}
Enter fullscreen mode Exit fullscreen mode

2. Initialize authentication in your App.

Add a Firebase instance in your project.

    private val firebaseInstance=FirebaseAuth.getInstance()
Enter fullscreen mode Exit fullscreen mode

3. Add Functionalities to Your App.

  • Sign In Functionality.
 fun signIn(email:String, password: String, context: Context) {
        firebaseInstance
            .signInWithEmailAndPassword(email, password)
            .addOnCompleteListener { task->
                if(task.isSuccessful) {
                    val user = firebaseInstance.currentUser?.email
                    Toast.makeText(context, "Sign in successful- $user", Toast.LENGTH_SHORT).show()
                }
                else{
                    Toast.makeText(context, "Failed, error: ${task.exception?.message}", Toast.LENGTH_SHORT).show()
                }
            }
    }
Enter fullscreen mode Exit fullscreen mode
  • Sign Up functionality.
 fun signUp(email:String, password: String, context: Context){
        firebaseInstance.createUserWithEmailAndPassword(email,password)
            .addOnCompleteListener { task->
                if(task.isSuccessful) {
                    val user = firebaseInstance.currentUser?.email
                    Toast.makeText(context, "Sign-up successful- $user", Toast.LENGTH_SHORT).show()
                }
                else{
                    Toast.makeText(context, "Failed, error: ${task.exception?.message}", Toast.LENGTH_SHORT).show()
                }
            }
    }
Enter fullscreen mode Exit fullscreen mode
  • Sign Out Functioality.
fun signOut(context: Context){
        firebaseInstance.signOut()
        Toast.makeText(context, "User signed out.", Toast.LENGTH_SHORT).show()
    }
Enter fullscreen mode Exit fullscreen mode
  • Manage User Session.
fun checkUserSession() {
    val user = firebaseAuth.currentUser
    if (user != null) {
        println("User already signed in: ${user.email}")
    } else {
        println("No user signed in.")
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion.

We have discussed the application of Firebase. We will learn more about Firebase in upcoming lecture, so stay tuned.

kotlin Article's
30 articles in total
Favicon
Using SVGs on Canvas with Compose Multiplatform
Favicon
Kotlin Generics Simplified
Favicon
Understanding Quick Sort in Kotlin : A Beginner's Guide
Favicon
Understanding Selection Sort in Kotlin: A Beginner's Guide
Favicon
Wednesday Links - Edition 2025-01-08
Favicon
Testing Pi Logic integrations.
Favicon
Understanding (a bit of) the Gradle Kotlin DSL
Favicon
Android TreeView(Hierarchy based) with Kotlin
Favicon
Creando un notebook con Jupyter y Kotlin
Favicon
Getting Started with Android Testing: Building Reliable Apps with Confidence (Part 3/3)
Favicon
Getting Started with Android Testing: Building Reliable Apps with Confidence (Part 2/3)
Favicon
Understanding Room Database in Android: A Beginner's Guide
Favicon
Fixing Rounded Navigation Bar Corner Padding in Jetpack Compose
Favicon
Getting Started with Android Testing: Building Reliable Apps with Confidence (Part 1/3)
Favicon
My conference year
Favicon
Authentication in Android Project with Firebase.
Favicon
Learn Firebase for Android Development from Scratch, a beginner guide.
Favicon
๐Ÿงน Improve Filtering with the Predicate Interface!
Favicon
How to make the best of a slow machine running on limited resources with a Windows environment as a Java Engineer
Favicon
How to implement detekt in Spring Boot + Kotlin + Gradle project
Favicon
How to Create and Publish an Android Library for Beginners
Favicon
Pub-sub Redis in Micronaut
Favicon
ISBN Stacks โ€” A look at a possible Spring Application implementation without annotations
Favicon
Protecting Applications with Kong security plugins and using StatsD to monitor system states โ€” A healthy camera story
Favicon
Configurable Kong API Gateway with Micronaut Services in Kotlin โ€” A very odd Yucca tribute concert
Favicon
Learning AWS with Localstack and Reactive Kotlin โ€” A stamps and coins implementation
Favicon
Coroutines, Distributed Cache, Resilience, and Replication in Kotlin โ€” Making a VMAโ€™s application
Favicon
From Paris to Berlin โ€” Creating Circuit-Breakers in Kotlin
Favicon
Understanding Merge Sort in Kotlin: A Beginner's Guide
Favicon
City Library โ€” An advanced guide to Circuit Breakers in Kotlin

Featured ones: