How to check if user has valid Auth Session Firebase iOS?

How to check if user has valid Auth Session Firebase iOS?

I wanna check if the user has still a valid session, before I present the Home View controller of my app. I use the latest Firebase API. I think if I use the legacy, I'll be able to know this.
Here's what I did so far:

I posted my question on Slack community of Firebase, no one is answering. I found this one, but this is for Android: https://groups.google.com/forum/?hl=el#!topic/firebase-talk/4HdhDvVRqHc
I tried reading the docs of Firebase for iOS, but I can't seem to comprehend it: https://firebase.google.com/docs/reference/ios/firebaseauth/interface_f_i_r_auth

I tried typing in Xcode like this:
FIRApp().currentUser()
FIRUser().getCurrentUser()

But I can't seem to find that getCurrentUser function.

Solutions/Answers:

Solution 1:

if FIRAuth.auth().currentUser != nil {
   presentHome()
} else {
   //User Not logged in
}

For updated SDK

if Auth.auth().currentUser != nil {

}

Solution 2:

Solution in Swift 3

override func viewDidLoad() {
    super.viewDidLoad()

    FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
        if user != nil {
            self.switchStoryboard()
        }
    }
}

Where switchStoryboard() is

func switchStoryboard() {
    let storyboard = UIStoryboard(name: "NameOfStoryboard", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "ViewControllerName") as UIViewController

    self.present(controller, animated: true, completion: nil)
}

Source

Solution 3:

if Auth.auth().currentUser?.uid != nil {

   //user is logged in

    }else{
     //user is not logged in
    }

Solution 4:

Solution in Swift 4

override func viewDidLoad() {
    super.viewDidLoad()
    setupLoadingControllerUI()
    checkIfUserIsSignedIn()
}

private func checkIfUserIsSignedIn() {

    Auth.auth().addStateDidChangeListener { (auth, user) in
        if user != nil {
            // user is signed in
            // go to feature controller 
        } else {
             // user is not signed in
             // go to login controller
        }
    }
}

Solution 5:

While you can see if there is such a user using Auth.auth().currentUser, this will only be telling you if there was a user authenticated, regardless of whether that users account still exists or is valid.

Related:  Firebase Android Default Setup and User Privacy

Complete Solution

The real solution to this should be using Firebase‘s re-authentication:

open func reauthenticate(with credential: AuthCredential, completion: UserProfileChangeCallback? = nil)

This assures (upon the launch of the application) that the previously signed in / authenticated user still in fact is and can be authenticated through Firebase.

let user = Auth.auth().currentUser    // Get the previously stored current user
var credential: AuthCredential

user?.reauthenticate(with: credential) { error in
  if let error = error {
    // An error happened.
  } else {
    // User re-authenticated.
  }
}

Solution 6:

override func viewDidLoad() {
FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
            // 2
            if user != nil {
                let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
                self.present(vc!, animated: true, completion: nil)
            }
        }
}

Source: https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2

Solution 7:

An objective-c solution would be (iOS 11.4):

[FIRAuth.auth addAuthStateDidChangeListener:^(FIRAuth * _Nonnull auth, FIRUser * _Nullable user) {
    if (user != nil) {
        // your logic
    }
}];

References