在不注销的情况下更新 isEmailVerified
Posted
技术标签:
【中文标题】在不注销的情况下更新 isEmailVerified【英文标题】:Update isEmailVerified without logging out 【发布时间】:2020-12-13 16:10:13 【问题描述】:我正在尝试在不注销应用程序的情况下更新 isEmailVerified 属性。到目前为止,我只能在退出应用程序时更新它。
这是我目前所拥有的:
struct ContentView: View
@EnvironmentObject var session: SessionStore
var body: some View
Group
if (self.session.session != nil)
let user = Auth.auth().currentUser
VStack
Text("Logged In As \(user?.email ?? "")")
Text("\(String(self.session.session!.isEmailVerified!))")
Button(action: user?.sendEmailVerification (error) in
)
Text("Verify email")
Button(action: session.signOut())
Text("Sign Out")
else
OnBoardingView()
.onAppear(perform:
session.listen()
)
和
struct User
var uid: String
var email: String?
var isEmailVerified: Bool?
init(uid: String, displayName: String?, email: String?, isEmailVerified: Bool?)
self.uid = uid
self.email = email
self.isEmailVerified = isEmailVerified
class SessionStore : ObservableObject
var didChange = PassthroughSubject<SessionStore, Never>()
var isLoggedIn = false didSet self.didChange.send(self)
@Published var session: User? didSet self.didChange.send(self)
var handle: AuthStateDidChangeListenerHandle?
init(session: User? = nil)
self.session = session
func listen ()
// monitor authentication changes using firebase
handle = Auth.auth().addStateDidChangeListener (auth, user) in
if let user = user
// if we have a user, create a new user model
print("Got user: \(user)")
self.isLoggedIn = true
DispatchQueue.main.async
self.session = User(
uid: user.uid,
displayName: user.displayName,
email: user.email,
isEmailVerified: user.isEmailVerified
)
else
// if we don't have a user, set our session to nil
self.isLoggedIn = false
self.session = nil
@discardableResult func signOut () -> Bool
do
try Auth.auth().signOut()
// self.isLoggedIn = false
// self.session = nil
return true
catch
return false
func signUp (
email: String,
password: String,
handler: @escaping AuthDataResultCallback
)
Auth.auth().createUser(withEmail: email, password: password, completion: handler)
func signIn (
email: String,
password: String,
handler: @escaping AuthDataResultCallback
)
Auth.auth().signIn(withEmail: email, password: password, completion: handler)
到目前为止,我得到的两个解决方案是我需要使用以下方法刷新用户对象:
Auth.auth()?.currentUser.reload()
或者我可以强制它使用以下方法获取新的 ID 令牌:
func getIDTokenResult(forcingRefresh forceRefresh: Bool, completion: ((FIRAuthTokenResult?, Error?) -> Void)? = nil)
但是,由于我是编码新手,我不确定是否会在我的代码中实现这一点。任何帮助,将不胜感激。谢谢
【问题讨论】:
既然您已经找到了两种有前途的方法,那么先尝试让它们发挥作用怎么样?然后,如果您在此过程中遇到困难,请在问题中展示您尝试过的内容,我们更有可能提供帮助。 @FrankvanPuffelen 我明白你在说什么。我是编码新手,所以我什至不确定从哪里开始。我已经根据这两种解决方案搜索了更详细的解释,但我没有找到任何东西。 【参考方案1】:我知道这是一个相当古老的讨论线程,但似乎没有人回答你的问题。如果我正确理解您的问题,您可以像这样刷新 .isEmailVerified 值(在 Swift 4 中):
Auth.auth().currentUser?.reload(completion: (error) in
if let error = error
// There was an error reloading the currentUser object
// Do something with the error here
else
// Success!
// Do anything you wanted to do with the refreshed .isEmailVerified in here!
print("The refreshed .isEmailVerified = \(Auth.auth().currentUser?.isEmailVerified)")
)
【讨论】:
以上是关于在不注销的情况下更新 isEmailVerified的主要内容,如果未能解决你的问题,请参考以下文章