Verifying the current Firebase user with a listener in iOS

I use Firebase Authorization to log in to the iOS app via Facebook and Google. I encode Swift. When the application starts, I need to check if the user is already signed up to present the correct ViewController (for example, if no one is signed up, I represent the login controller, otherwise I represent the Home View controller). If I use the “lightweight” solution offered by Firebase, it means

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

Therefore, if the current user is not equal to zero, this happens just so that a Firebase warning may occur ( https://firebase.google.com/docs/auth/ios/manage-users ).

"Note: currentUser may also be nil because the auth object did not complete initialization. If you use a listener to track login status, you do not need to handle this case."

So, I would like to implement a listener as suggested in the manual:

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

The listener will also process the intermediate status so that it is triggered when the Auth object is created. Point, I really can't get it to work correctly. Can someone help me use this listener to check if a user is registered?

thanks

+6
source share
2 answers

I implemented it as follows:

 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 after checking you do not need a User object, you can replace if let user = user with a logical test, for example:

 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 that I used to check if the user was signed up, it was enough to put it at the beginning of viewDidLoad in a specific view controller. But if you have cases where you need to check every time you enter a specific controller, then it is better to put it at the top of viewDidAppear . But I think in most cases you only need to check once if the user enters the view

+9
source

If you configure StateDidChangeListener in application:didFinishLaunchingWithOptions , you will notice that it fires once when the listener is connected (to set the initial state to nil during initialization), and then again after initialization is completed (potentially not nil ). This is the intended behavior, but is actually impractical if you configure it at an early stage.

An alternative to using a listener is to use NotificationCenter . This will work after initialization is complete:

 NotificationCenter.default.addObserver(forName: NSNotification.Name.AuthStateDidChange, object: Auth.auth(), queue: nil) { _ in let user = Auth.auth().currentUser } 
+1
source

Source: https://habr.com/ru/post/1262487/


All Articles