如何检查用户电子邮件是不是已验证?
Posted
技术标签:
【中文标题】如何检查用户电子邮件是不是已验证?【英文标题】:How to check if user email verified?如何检查用户电子邮件是否已验证? 【发布时间】:2020-11-18 08:35:59 【问题描述】:我想验证用户电子邮件,为此我发送验证链接,关注它,并期望 isEmailVerified 属性将其值更改为 true,但这不会发生(即使我重新打开应用程序)。仅当我注销并再次登录时,该属性才会将其值更改为 true。
这种行为的解释是什么?如何在不离开账号的情况下查看用户邮箱进行验证?
ContentView.swift:
struct ContentView: View
@EnvironmentObject var session: SessionStore
var body: some View
Group
if (self.session.session != nil)
VStack
Text("Logged In As \(Auth.auth().currentUser?.email ?? "")")
Text("\(String(self.session.session!.isEmailVerified!))")
//This also print false:
//Text("\(String((Auth.auth().currentUser?.isEmailVerified)!))")
Button(action: Auth.auth().currentUser?.sendEmailVerification (error) in
// ...
)
Text("Verify email")
Button(action: session.signOut())
Text("Sign Out")
else
SignInView()
.onAppear(perform:
session.listen()
)
SessionStore.swift:
class SessionStore : ObservableObject
var didChange = PassthroughSubject<SessionStore, Never>()
@Published var session: User? didSet self.didChange.send(self)
var handle: AuthStateDidChangeListenerHandle?
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)")
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.session = nil
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)
func signOut () -> Bool
do
try Auth.auth().signOut()
self.session = nil
return true
catch
return false
【问题讨论】:
你能显示SessionStore
和Session
的代码吗? isEmailVerified
在哪里更新或应该更新?
@Asperi 是的,我已经更新了问题。 isEmailVerified 不是在 listen 方法中更新了吗?
【参考方案1】:
要解决你需要刷新用户对象的问题:
Auth.auth()?.currentUser.reload()
然后您可以检查任何更新的数据
【讨论】:
以上是关于如何检查用户电子邮件是不是已验证?的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 和 jwt-auth。如何检查用户电子邮件是不是经过验证