Firebase - iOS Swift: FIRAuth.auth().signOut() not signing out current user

后端 未结 6 1304
栀梦
栀梦 2020-12-18 04:24

I\'m building an app using Firebase with an initial SignInViewController that loads a sign in page for users to authenticate with email which triggers the follo

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-18 05:14

    I actually had this issue as well. I was also logging out the user (as you are) with the method's provided by Firebase but when I printed to the console it said that I still had a optional user.

    I had to change the logic of setting the current user so that it is always configured by the authentication handler provided by Firebase:

    var currentUser: User? = Auth.auth().currentUser
    var handle: AuthStateDidChangeListenerHandle!
    
    init() {
    
        handle = Auth.auth().addStateDidChangeListener { (auth, user) in
            self.currentUser = user
    
            if user == nil {
                 UserDefaults.standard.setValue(false, forKey: UserDefaults.loggedIn)
            } else {  
                 UserDefaults.standard.setValue(true, forKey: UserDefaults.loggedIn)
            }
    
        }
    
    }
    

    As long as you are referencing the current user from this handle, it will update the current user no matter the authentication state.

提交回复
热议问题