[英]How to check if user has valid Auth Session Firebase iOS?
I wanna check if the user has still a valid session, before I present the Home View controller of my app.在我展示我的应用程序的主视图 controller 之前,我想检查用户是否仍然拥有有效的 session。 I use the latest Firebase API. I think if I use the legacy, I'll be able to know this.
我用的是最新的Firebase API。我想如果我用legacy的话,就可以知道这个了。
Here's what I did so far:这是我到目前为止所做的:
I posted my question on Slack community of Firebase, no one is answering.我在 Firebase 的 Slack 社区上发布了我的问题,没有人回答。 I found this one, but this is for Android: https://groups.google.com/forum/?hl=el#!topic/firebase-talk/4HdhDvVRqHc
我找到了这个,但这是针对 Android 的: https://groups.google.com/forum/?hl=el#!topic/firebase-talk/4HdhDvVRqHc
I tried reading the docs of Firebase for iOS, but I can't seem to comprehend it: https://firebase.google.com/docs/reference/ios/firebaseauth/interface_f_i_r_auth我试着阅读 Firebase 的 iOS 的文档,但我似乎无法理解它: https://firebase.google.com/docs/reference/ios/firebaseauth/interface_f_i_r_auth
I tried typing in Xcode like this:我试着像这样输入 Xcode:
FIRApp().currentUser()
FIRUser().getCurrentUser()
But I can't seem to find that getCurrentUser function.但我似乎找不到 getCurrentUser function。
if FIRAuth.auth().currentUser != nil {
presentHome()
} else {
//User Not logged in
}
For updated SDK 对于更新的SDK
if Auth.auth().currentUser != nil {
}
Updated answer 更新的答案
Solution for latest Firebase SDK - DOCS 解决方案最新的火力地堡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 Swift 3中的解决方案
override func viewDidLoad() {
super.viewDidLoad()
FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
if user != nil {
self.switchStoryboard()
}
}
}
Where switchStoryboard()
is 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)
}
if Auth.auth().currentUser?.uid != nil {
//user is logged in
}else{
//user is not logged in
}
Solution in Swift 4 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
}
}
}
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. 虽然您可以使用
Auth.auth().currentUser
查看是否有这样的用户,但这只会告诉您是否有经过身份验证的用户,无论该用户帐户是否仍然存在或有效。
The real solution to this should be using Firebase
's re-authentication: 真正的解决方案应该是使用
Firebase
的重新身份验证:
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. 这样可以确保(在应用程序启动时)以前登录/已通过身份验证的用户实际上仍然 是并且可以通过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)
}
}
}
Source: https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2 资料来源: https : //www.raywenderlich.com/139322/firebase-tutorial-getting-started-2
An objective-c solution would be (iOS 11.4): 一个Objective-C解决方案将是(iOS 11.4):
[FIRAuth.auth addAuthStateDidChangeListener:^(FIRAuth * _Nonnull auth, FIRUser * _Nullable user) {
if (user != nil) {
// your logic
}
}];
All the provided answers only check on currentUser
.所有提供的答案仅检查
currentUser
。 But you could check the auth session by simple user reload like below:但是您可以通过简单的用户重新加载来检查身份验证 session,如下所示:
// 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!")
}
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.