在viewDidDisappear中删除Firebase身份验证 [英] Removing Firebase authState upon viewDidDisappear
问题描述
我正在使用Firebase的新框架,并试图在登录和注册VC中分别监视用户的登录状态。问题是,如果登录状态在注册视图上更改,那么登录视图上的身份验证状态也会被调用。我的问题是,如何删除身份验证状态?我在Firebase网站上找到了这个语法,但是在考虑我的身份验证状态的代码时,有些困惑:
FIRAuth.auth()?addAuthStateDidChangeListener {auth,用户在
如果让用户=用户{
//用户登录
print(LOGGED IN !!!! ::: :\(theUser))
self.dismissViewControllerAnimated(true,completion:nil)
} else {
//没有用户登录
print(首先登录。)
}
}
用于删除auth,但是不确定要传入的内容。
$ $ $ $ $ $ $ $ $ $ $ $ code>
说我传递了一个 FIRAuthStateDidChangeListenerHandle
,但是我怎么获得这个,或者我重写我的authState代码不同?
pre $ self_authListener = FIRAuth.auth()?addAuthStateDidChangeListener {auth,用户在
中,如果让用户=用户{
//用户登录
print(LOGGED IN !!!!
self.dismissViewControllerAnimated(true,completion:nil)
} else {
//没有用户登录
print(
code $
并将其删除稍后
FIRAuth.auth()?removeAuthStateDidChangeListener(self.authListener)
I'm using Firebase's new framework and I'm attempting to monitor the login state of the user on both the Login and Signup VC's separately. The problem is if the login state changes on the SignUp view the Auth State on the Login view gets called as well. My question is, how do I remove the auth state? I found the syntax on the Firebase website but am a little confused on what to pass in considering my code for the auth state:
FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
if let theUser = user {
// User is signed in.
print("LOGGED IN!!!! :::: \(theUser)")
self.dismissViewControllerAnimated(true, completion: nil)
} else {
// No user is signed in.
print("Need to login first.")
}
}
Code to use to remove the auth, but unsure what to pass in.
FIRAuth.auth()?.removeAuthStateDidChangeListener(FIRAuthStateDidChangeListenerHandle)
Says I pass in a FIRAuthStateDidChangeListenerHandle
, but how do I obtain this, or do I rewrite my authState code differently?
Just store the auth in a variable
self.authListener = FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
if let theUser = user {
// User is signed in.
print("LOGGED IN!!!! :::: \(theUser)")
self.dismissViewControllerAnimated(true, completion: nil)
} else {
// No user is signed in.
print("Need to login first.")
}
}
and remove it later
FIRAuth.auth()?.removeAuthStateDidChangeListener(self.authListener)
这篇关于在viewDidDisappear中删除Firebase身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!