[英]Send an email verfication email to a new Firebase User in Swift
I have my app set up to successfully login or create an account with Firebase using an email address and a password.我将我的应用程序设置为使用电子邮件地址和密码成功登录或创建 Firebase 帐户。 What I want to do now is to check if the user has verified their email, and if not send them the Verification email that Firebase allows us to write.
我现在要做的是检查用户是否已经验证了他们的电子邮件,如果没有,则向他们发送 Firebase 允许我们编写的验证电子邮件。
@IBAction func createAccountTapped(_ sender: Any) {
if let email = emailTextfield.text, let password = passwordTextfield.text {
Auth.auth().createUser(withEmail: email, password: password, completion: { user, error in
if let firebaseError = error {
print(firebaseError.localizedDescription)
return
}
self.presentTabBar()
})
}
}
@IBAction func loginTapped(_ sender: Any) {
if let email = emailTextfield.text, let password = passwordTextfield.text {
Auth.auth().signIn(withEmail: email, password: password, completion: {(user, error) in
if let firebaseError = error {
print(firebaseError.localizedDescription)
return
}
self.presentTabBar()
})
}
}
Here are my "createaccount" and "login" functions.这是我的“创建帐户”和“登录”功能。
My problem is that I don't know how to implement this functionality, and where to call those functions.我的问题是我不知道如何实现这个功能,也不知道在哪里调用这些函数。
Can you show me what the function to send this email is, and what the function to check if the email is verified?你能告诉我发送这封电子邮件的功能是什么,以及检查电子邮件是否通过验证的功能是什么?
You can send the verification mail just after creating the account:您可以在创建帐户后立即发送验证邮件:
(Don't forget to create an additional button/action so the user may ask for the verification mail again) (不要忘记创建一个额外的按钮/操作,以便用户可以再次请求验证邮件)
private var authUser : User? {
return Auth.auth().currentUser
}
public func sendVerificationMail() {
if self.authUser != nil && !self.authUser!.isEmailVerified {
self.authUser!.sendEmailVerification(completion: { (error) in
// Notify the user that the mail has sent or couldn't because of an error.
})
}
else {
// Either the user is not available, or the user is already verified.
}
}
Combining with your code:结合您的代码:
@IBAction func createAccountTapped(_ sender: Any) {
if let email = emailTextfield.text, let password = passwordTextfield.text {
Auth.auth().createUser(withEmail: email, password: password, completion: { user, error in
if let firebaseError = error {
print(firebaseError.localizedDescription)
return
}
sendVerificationMail()
self.presentTabBar()
})
}
}
To check if the user has verified:检查用户是否已验证:
@IBAction func loginTapped(_ sender: Any) {
if let email = emailTextfield.text, let password = passwordTextfield.text {
Auth.auth().signIn(withEmail: email, password: password, completion: {(user, error) in
if let firebaseError = error {
print(firebaseError.localizedDescription)
return
}
if user != nil && !user!.isEmailVerified {
// User is available, but their email is not verified.
// Let the user know by an alert, preferably with an option to re-send the verification mail.
}
self.presentTabBar()
})
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.