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

前端 未结 7 2414
生来不讨喜
生来不讨喜 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: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.
      }
    }
    

提交回复
热议问题