![](/img/trans.png)
[英]How do you send a Firebase verification email (so that a user can verify their email) to a user in React Native?
[英]Send an email verfication email to a new Firebase User in Swift
我將我的應用程序設置為使用電子郵件地址和密碼成功登錄或創建 Firebase 帳戶。 我現在要做的是檢查用戶是否已經驗證了他們的電子郵件,如果沒有,則向他們發送 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()
})
}
}
這是我的“創建帳戶”和“登錄”功能。
我的問題是我不知道如何實現這個功能,也不知道在哪里調用這些函數。
你能告訴我發送這封電子郵件的功能是什么,以及檢查電子郵件是否通過驗證的功能是什么?
您可以在創建帳戶后立即發送驗證郵件:
(不要忘記創建一個額外的按鈕/操作,以便用戶可以再次請求驗證郵件)
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.
}
}
結合您的代碼:
@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()
})
}
}
檢查用戶是否已驗證:
@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.