DEV Community

Harsh Lade
Harsh Lade

Posted on • Edited on

1

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.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay