如何检查用户是否具有有效的身份验证会话Firebase iOS?

prdp8dxp  于 2023-01-09  发布在  iOS
关注(0)|答案(8)|浏览(143)

在展示应用的主视图控制器之前,我想检查用户是否仍有有效会话。我使用最新的Firebase API。我想如果我使用旧版,我将能够知道这一点。
以下是我目前所做的:

我试着像这样输入Xcode:

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

但我似乎找不到getCurrentUser函数。

puruo6ea

puruo6ea1#

if FIRAuth.auth().currentUser != nil {
   presentHome()
} else {
   //User Not logged in
}

对于更新的SDK

if Auth.auth().currentUser != nil {

}
fbcarpbf

fbcarpbf2#

    • 更新答复**

最新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)
        }
    }
    • 原始溶液**

Swift 3中的解决方案

override func viewDidLoad() {
    super.viewDidLoad()

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

其中switchStoryboard()

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

xxslljrj

xxslljrj3#

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
        }
    }
}
vjrehmav

vjrehmav4#

if Auth.auth().currentUser?.uid != nil {

   //user is logged in

    }else{
     //user is not logged in
    }
cngwdvgl

cngwdvgl5#

虽然您 * 可以 * 看到是否有这样的用户使用Auth.auth().currentUser,但这只会告诉您是否用户经过身份验证,而不管该用户的帐户是否仍然存在或有效。

完整解决方案

真实的的解决方案应该是使用Firebase的重新身份验证:

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

这可确保(在启动应用程序时)之前登录/认证的用户 * 实际上*仍然通过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.
  }
}
hfwmuf9z

hfwmuf9z6#

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)
            }
        }
}

来源:https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2

yx2lnoni

yx2lnoni7#

目标c解决方案是(iOS 11.4):

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

kpbwa7wx8#

所有提供的答案只能在currentUser上检查。但您可以通过简单的用户重新加载来检查auth会话,如下所示:

// Run on the background thread since this is just a Firestore user reload, But you could also directly run on the main thread.

    DispatchQueue.global(qos: .background).async {
        Auth.auth().currentUser?.reload(completion: { error in
            if error != nil {
                DispatchQueue.main.async {
                    // Authentication Error
                    // Do the required work on the main thread if necessary 
                }
            } else {
                log.info("User authentication successfull!")
            }
        })
    }

相关问题