在展示应用程序的主视图控制器之前检查用户是否仍有有效的会话。我使用的是最新的 Firebase API。我想如果我使用遗产,我将能够知道这一点。
这是我到目前为止所做的:
我在Firebase的Slack社区上发布了我的问题,没有人回答。我找到了这个,但这是针对Android的:https://groups.google.com/forum/?hl=el#!topic/firebase-talk/4HdhDvVRqHc
我尝试阅读 Firebase for iOS 的文档,但我似乎无法理解它:https://firebase.google.com/docs/reference/ios/firebaseauth/interface_f_i_r_auth
我尝试像这样输入Xcode:
FIRApp().currentUser()
FIRUser().getCurrentUser()
但我似乎找不到getCurrentUser功能。
if FIRAuth.auth().currentUser != nil {
presentHome()
} else {
//User Not logged in
}
对于更新的开发工具包
if Auth.auth().currentUser != nil {
}
更新了答案
适用于最新 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)
}
源
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
}
}
}
if Auth.auth().currentUser?.uid != nil {
//user is logged in
}else{
//user is not logged in
}
虽然您可以查看是否有这样的用户使用 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.
}
}
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
一个 objective-c 解决方案是 (iOS 11.4):
[FIRAuth.auth addAuthStateDidChangeListener:^(FIRAuth * _Nonnull auth, FIRUser * _Nullable user) {
if (user != nil) {
// your logic
}
}];
提供的答案都只检查currentUser
。但是您可以通过简单的用户重新加载来检查身份验证会话,如下所示:
// 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!")
}
})
}