/firebase-tutorials

How to sign out a user in Firebase?

Learn how to sign out a user in Firebase across web, Android, and iOS. Follow our step-by-step guide with code examples and UI best practices.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to sign out a user in Firebase?

 

Step 1: Set Up Your Firebase Project

 

  • First, ensure you have a Firebase project set up. Go to the Firebase Console, create a new project, or select an existing one.

 

Step 2: Integrate Firebase into Your App

 

  • Integrate Firebase into your mobile or web application. For Android and iOS apps, add the Firebase SDK to your project following the Firebase setup documentation for Android or iOS.

  • For web apps, include the Firebase library in your HTML file. If you haven't already added Firebase to your web app, use the following script in your HTML:

    ```html

    ```

 

Step 3: Sign Out a User Using Firebase Authentication

 

  • To sign out a user in Firebase Authentication, you need to call the signOut() method provided by the Firebase Authentication SDK. The process varies slightly between Android, iOS, and Web.

  • For Web Applications:

    ```html

    ```

    Add a button in your HTML that calls this function:

    ```html

    ```

  • For Android Applications:

    First, ensure you have added Firebase Auth dependency in your build.gradle file:

    ```gradle
    implementation 'com.google.firebase:firebase-auth:21.0.1'
    ```

    Use the following code in your activity class to sign out:

    ```java
    FirebaseAuth mAuth = FirebaseAuth.getInstance();

    private void signOutUser() {
    mAuth.signOut();
    Toast.makeText(getApplicationContext(), "User signed out successfully.", Toast.LENGTH_SHORT).show();
    // Redirect to login activity or perform other operations
    }
    ```

  • For iOS Applications:

    Ensure you have integrated Firebase SDK using CocoaPods, and include Firebase Auth in your Podfile:

    ```ruby
    pod 'Firebase/Auth'
    ```

    Run pod install to update your project. To sign out, use the following code in your view controller:

    ```swift
    import FirebaseAuth

    func signOutUser() {
    do {
    try Auth.auth().signOut()
    print("User signed out successfully.")
    // Redirect to login view controller or perform other operations
    } catch let signOutError as NSError {
    print("Error signing out: %@", signOutError)
    }
    }
    ```

 

Step 4: Test the Sign-Out Functionality

 

  • Ensure that the sign-out functionality works by running your application and attempting to sign out.

  • For Mobile Applications (Android/iOS), check that the user is redirected to the login screen or that the UI updates appropriately after signing out.

  • For Web Applications, verify that the user session is ended and UI responds appropriately. This often involves redirecting the user to a login page or updating the UI to reflect that the user is no longer signed in.

 

Step 5: Handle Post Sign-Out UI/UX

 

  • Design your application to provide a seamless user experience after sign-out. This includes:

  • Navigating back to a login screen.

  • Clearing any user data from the state/UI if necessary.

  • Providing feedback to the user, such as a message indicating a successful sign-out.

  • For instance, you may use Toast messages on Android, Alerts in iOS, or DOM updates on the Web to inform the user of a successful sign-out.

  • Consider additional security practices like clearing stored sensitive information locally if applicable (e.g., tokens, caches).

 

By following the above steps, you can successfully implement and test a sign-out functionality using Firebase Authentication across multiple platforms.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022