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

I want to check if the user has a valid session before I submit the Home View controller of my application. I use the latest Firebase API. I think that if I use this legacy, I can find out.

Here is what I have done so far:

I tried typing in Xcode like this:

FIRApp().currentUser() FIRUser().getCurrentUser() 

But I can not find this getCurrentUser function.

+10
source share
7 answers
 if FIRAuth.auth().currentUser != nil { presentHome() } else { //User Not logged in } 

For updated SDK

 if Auth.auth().currentUser != nil { } 
+19
source

Updated Answer

Solution for Latest Firebase SDK - DOCS

  // save a ref to the handler private var authListener: AuthStateDidChangeListenerHandle? // Check for auth status some where override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) authListener = Auth.auth().addStateDidChangeListener { (auth, user) in if let user = user { // User is signed in // let the user in? if user.isEmailVerified { // Optional - check if the user verified their email too // let the user in? } } else { // No user } } } // Remove the listener once it no longer needed deinit { if let listener = authListener { Auth.auth().removeStateDidChangeListener(authListener) } } 

Original solution

Solution in Swift 3

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

Where switchStoryboard() is located

 func switchStoryboard() { let storyboard = UIStoryboard(name: "NameOfStoryboard", bundle: nil) let controller = storyboard.instantiateViewController(withIdentifier: "ViewControllerName") as UIViewController self.present(controller, animated: true, completion: nil) } 

A source

+9
source
 if Auth.auth().currentUser?.uid != nil { //user is logged in }else{ //user is not logged in } 
+5
source

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 } } } 
+4
source

Although you can see if there is such a user that uses Auth.auth().currentUser , this will only tell you if the user has been authenticated, regardless of whether this user account exists or exists.


Complete solution

The real solution for this should be to use Firebase re-authentication:

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

This ensures (after starting the application) that the previously signed user / authenticated user is still 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. } } 
+3
source
 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

0
source

The solution to task-c would be (iOS 11.4):

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

All Articles