Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking Firebase current signed-in user via Listener in iOS

I've implement Firebase Authorization to login on my iOS app via Facebook and Google. I'm coding Swift. When the app starts up I need to check whether a user is already signed-in in order to present the proper ViewController (e.g., if nobody is signed in I present the Login View Controller, otherwise I present the Home View Controller). If I use the "easy" solution offered by Firebase, meaning

if FIRAuth.auth()?.currentUser != nil {
  // User is signed in.
  // ...
} else {
  // No user is signed in.
  // ...
}

So checking if the current user is not nil, it happens exactly what the Firebase guide (https://firebase.google.com/docs/auth/ios/manage-users) alerts might happen meaning

"Note: currentUser might also be nil because the auth object has not finished initializing. If you use a listener to keep track of the user's sign-in status, you don't need to handle this case."

So I would like to implement the listener as suggested in the guide:

handle = FIRAuth.auth()?.addStateDidChangeListener() { (auth, user) in
  // ...
}

The listener will handle also intermediate status so that it is triggered when the Auth object is created. Point is I really cannot make it to work properly. Anybody can help me to use this listener in order to check if a user is logged in?

Thanks

like image 545
Freddie Mash Avatar asked Jan 08 '17 09:01

Freddie Mash


People also ask

How do you check if a user is already signed in Firebase?

You can easily detect if the user is logged or not by executing: var user = firebase. auth().

What does Firebase auth () CurrentUser return?

If a user isn't signed in, CurrentUser returns null. Note: CurrentUser might also return null because the auth object has not finished initializing. If you use a listener to keep track of the user's sign-in status, you don't need to handle this case.


1 Answers

I've implemented it like this:

FIRAuth.auth()?.addStateDidChangeListener { auth, user in
  if let user = user {
    // User is signed in. Show home screen
  } else {
    // No User is signed in. Show user the login screen
  }
}

If you don't need the User object after checking, you can replace if let user = user with a boolean test, like this:

FIRAuth.auth()?.addStateDidChangeListener { auth, user in
  if user != nil {
    // User is signed in. Show home screen
  } else {
    // No User is signed in. Show user the login screen
  }
}

Where to put the listener (from the comments):

For the cases I used to check if a user is signed in, it was enough to put it at the beginning of viewDidLoad in the specific view controller. But if you have any cases where you need to check every time you enter the specific view controller then it would be better to put it at the beginning of viewDidAppear. But I think in most cases you need to check only once, if the user enters the view

like image 83
ronatory Avatar answered Sep 22 '22 20:09

ronatory