ale*_*ier 6 ios firebase swift firebase-authentication
我正在使用Firebase构建一个应用程序,该应用程序使用一个初始文件SignInViewController加载登录页面,供用户通过电子邮件进行身份验证,从而触发以下方法:
@IBAction func didTapSignIn(sender: AnyObject) {
let email = emailField.text
let password = passwordField.text
FIRAuth.auth()?.signInWithEmail(email!, password: password!) { (user, error) in
if let error = error {
print(error.localizedDescription)
return
}
self.signedIn(user!)
}
}
func signedIn(user: FIRUser?) {
AppState.sharedInstance.displayName = user?.displayName ?? user?.email
AppState.sharedInstance.signedIn = true
NSNotificationCenter.defaultCenter().postNotificationName(Constants.NotificationKeys.SignedIn, object: nil, userInfo: nil)
performSegueWithIdentifier(Constants.Segues.SignInToHome, sender: nil)
}
Run Code Online (Sandbox Code Playgroud)
该SignInViewController还检查是否有缓存的当前用户,当应用程序启动,如果是这样,迹象表明,用户在:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
//Synchronously gets the cached current user, or null if there is none.
if let user = FirebaseConfigManager.sharedInstance.currentUser {
self.signedIn(user)
}
}
Run Code Online (Sandbox Code Playgroud)
用户登录后,应用程序将转到HomeScreenViewController显示导航栏左上角的"注销"按钮的应用程序.当用户点击"注销"按钮时,该用户应该退出,应用程序应该SignInViewController使用以下方法转回:
@IBAction func didTapSignOut(sender: UIBarButtonItem) {
print("sign out button tapped")
let firebaseAuth = FIRAuth.auth()
do {
try firebaseAuth?.signOut()
AppState.sharedInstance.signedIn = false
dismissViewControllerAnimated(true, completion: nil)
} catch let signOutError as NSError {
print ("Error signing out: \(signOutError)")
} catch {
print("Unknown error.")
}
}
Run Code Online (Sandbox Code Playgroud)
当我点击"退出"按钮时,该didTapSignOut方法被调用并执行.但是,在try firebaseAuth?.signOut()执行代码行之后,当前用户应该是nil.但是当我在Xcode控制台中打印出当前用户时,当前用户仍然登录:
po FIRAuth.auth()?.currentUser
? Optional<FIRUser>
- Some : <FIRUser: 0x7fde43540f50>
Run Code Online (Sandbox Code Playgroud)
由于当前用户在firebaseAuth?.signOut()被调用后没有注销,一旦应用程序回到SignInViewController应用程序仍然认为存在缓存的当前用户,以便用户再次登录.
这可能是钥匙串问题吗?
是否与NSNotificationCenter.defaultCenter().postNotificationName被召唤有关?
我的代码直接来自Google Firebase Swift Codelab,所以我不确定它为什么不起作用:https: //codelabs.developers.google.com/codelabs/firebase-ios-swift/#4
您可以在viewDidAppear视图控制器的方法中添加侦听器,如下所示:
FIRAuth.auth()?.addStateDidChangeListener { auth, user in
if let user = user {
print("User is signed in.")
} else {
print("User is signed out.")
}
}
Run Code Online (Sandbox Code Playgroud)
这使您可以在用户的身份验证状态更改时执行代码。由于signOutFirebase中的方法没有完成处理程序,因此您可以侦听事件。
| 归档时间: |
|
| 查看次数: |
8286 次 |
| 最近记录: |