Dec 19, 2019

Firebase Auth State Listeners iOS

Receive notification when firebase users auth state changes on iOS.

Firebase auth lets you pass a handler function when the auth state changes.

In my use case I need to receive state change alerts. My user might be disabled by an admin or a bot, or the account is stopped for security reasons. But, I need to update user interfaces and data code to match that the user is logged out.

Fortunately its easy with firebase

Somewhere in your startup code -I use a container view for all my views, but the AppDelegate or SceneManager works well here- put this snippet in.

func setAuthListener() {
    Auth.auth().addStateDidChangeListener { (auth, user) in
        guard let user else {
            // The user is either logged out, or something went wrong. 
            // You can use the Auth.auth().currentUser.reload() function 
            // to try to find the error using the FIRErrorCode enum
            return
        }

        // Otherwise, we now have a signed in user. Usually that just means 
        // we carry on, but you could put some custom handler here too.
    }
}

Then just fire that function somewhere at the start of your app, or in a container controller like we made in this article.