Android Firebase Authentication using Kotlin, MVVM, LiveData, and View Binding (part V)

Alexandru Rotariu
3 min readFeb 25, 2023

--

Part V: Implementing logout functionality using Firebase Authentication

Welcome to the fifth post in our series on Android Firebase Authentication using Kotlin, MVVM, LiveData and View Binding!

This post is part of a longer series:

So, let’s dive into the content of this tutorial.

Adding the logout button to the user interface

We will now add a logout button to the user interface. This button will allow the user to sign out of their account when they are done using the app. To do this, we will add a button to the user interface layout file.

  1. Open the layout file where you want to add the logout button.
  2. Add a Button element to the layout file with the text “Logout”.
  3. Assign an ID to the button so that we can reference it in the code.
<?xml version="1.0" encoding="utf-8"?>
<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">

<TextView
android:id="@+id/welcome_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome, User!"
android:textSize="24sp"
android:textStyle="bold"
android:layout_marginBottom="16dp"/>

<Button
android:id="@+id/logout_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logout"
android:textAllCaps="false"/>

</LinearLayout>

Now that we have added the logout button to the user interface, let’s move on to implementing the logout functionality.

Implementing the logout functionality using Firebase Authentication

To implement the logout functionality, we will use the Firebase Authentication API. Here is an example of how to implement the logout functionality:

  1. Get an instance of the FirebaseAuth class.
  2. Call the signOut() method on the FirebaseAuth instance.
  3. Add an OnCompleteListener to the signOut() method to handle the result.

Here’s the code for implementing the logout functionality:

FirebaseAuth.getInstance().signOut()
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// User has been signed out
} else {
// An error occurred
}
}

Updating the UI after logout using LiveData

Now that we have implemented the logout functionality, we need to update the UI to reflect the user’s authentication state. To do this, we can use LiveData. Here is an example of how to set up LiveData to observe the authentication state:

  1. Create a LiveData<Boolean> object to represent the authentication state.
  2. Observe the authentication state using the Firebase Authentication API.
  3. Update the UI based on the authentication state.

Here’s the code for updating the UI after logout using LiveData:

// Add this in your View Model
private val _isAuthenticated = MutableLiveData<Boolean>()
val isAuthenticated: LiveData<Boolean> = _isAuthenticated
// Observe the authentication state using the
// Firebase Authentication API in your logout method
FirebaseAuth.getInstance().signOut()
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
isAuthenticated.value = firebaseAuth.currentUser != null
} else {
// An error occurred
}
}
// In your activity, update the UI based on the authentication state
isAuthenticatedLiveData.observe(this, { isAuthenticated ->
if (isAuthenticated) {
// User is authenticated
} else {
// User is not authenticated
}
})

With these steps, we have implemented the logout functionality and updated the UI to reflect the user’s authentication state. In the next section, we will learn about handling authentication state changes using Firebase Authentication.

Next post - Part VI: Handling authentication state changes using Firebase Authentication

--

--