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

前端 未结 7 2242
生来不讨喜
生来不讨喜 2021-02-19 04:53

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 ab

相关标签:
7条回答
  • 2021-02-19 05:36

    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's 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

    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

    0 讨论(0)
  • 2021-02-19 05:42
    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 讨论(0)
  • 2021-02-19 05:45

    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
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-19 05:49
    if FIRAuth.auth().currentUser != nil {
       presentHome()
    } else {
       //User Not logged in
    }
    

    For updated SDK

    if Auth.auth().currentUser != nil {
    
    }
    
    0 讨论(0)
  • 2021-02-19 05:55

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

    [FIRAuth.auth addAuthStateDidChangeListener:^(FIRAuth * _Nonnull auth, FIRUser * _Nullable user) {
        if (user != nil) {
            // your logic
        }
    }];
    
    0 讨论(0)
  • 2021-02-19 05:56

    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.


    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.
      }
    }
    
    0 讨论(0)
提交回复
热议问题