【问题标题】:Firebase email verification always return false user not verified. iOSFirebase 电子邮件验证始终返回未验证的虚假用户。 iOS
【发布时间】:2019-04-04 15:59:48
【问题描述】:

我正在实施 Firebase 电子邮件验证,但是当用户收到一封电子邮件时,它会重定向到应用程序,这很好。当我尝试使用经过验证的电子邮件 Auth.auth().currentUser?.isEmailVerified 登录时,总是返回 false。以下是我的代码:

class ViewController: UIViewController {

@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var signInBtn: UIButton!
var link: String!

override func viewDidLoad() {
    super.viewDidLoad()
    if let link = UserDefaults.standard.value(forKey: "Link") as? String{
        self.link = link
        signInBtn.isEnabled = true
    }
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

@IBAction func didTapSignInWithEmailLink(_ sender: AnyObject) {
    if let email = self.emailTextField.text {
        Auth.auth().signIn(withEmail: email, link: self.link) { (user, error) in
            if let error = error {
                print("\(error.localizedDescription)")
                return
            }
        }
    } else {
        print("Email can't be empty")
    }

    print(Auth.auth().currentUser?.email)
    if Auth.auth().currentUser?.isEmailVerified == true {
        print("User verified")
    } else {
        print("User not verified")
    }

}
@IBAction func didTapSendSignInLink(_ sender: AnyObject) {
    if let email = emailTextField.text {
        let actionCodeSettings = ActionCodeSettings()
        actionCodeSettings.url = URL(string: "https://example.firebaseapp.com")

        // The sign-in operation has to always be completed in the app.
        actionCodeSettings.handleCodeInApp = true
        actionCodeSettings.setIOSBundleID(Bundle.main.bundleIdentifier!)
        actionCodeSettings.setAndroidPackageName("com.example.android",
                                                 installIfNotAvailable: false, minimumVersion: "12")
        Auth.auth().sendSignInLink(toEmail: email, actionCodeSettings: actionCodeSettings) { error in
            if let error = error {
                print("\(error.localizedDescription)")
                return
            }
            print("Check your email for link")
        }
    } else {
        print("Email cant be empty")
    }
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

【问题讨论】:

    标签: ios swift firebase email-verification


    【解决方案1】:

    我找到了我们需要重新加载用户的解决方案,这将更新当前用户状态。在 didTapSignInWithEmailLink 按钮功能中调用 Auth.auth.currentUser.reload:

    Auth.auth().currentUser?.reload(completion: { (error) in
                if error == nil{
                    if let email = self.emailTextField.text {
                        Auth.auth().signIn(withEmail: email, link: self.link) { (user, error) in
                            if let error = error {
                                print("\(error.localizedDescription)")
                                return
                            }
                        }
                    } else {
                        print("Email can't be empty")
                    }
    
                    print(Auth.auth().currentUser?.email)
                    if Auth.auth().currentUser?.isEmailVerified == true {
                        print("User verified")
                    } else {
                        print("User not verified")
                    }
                } else {
                    print(error?.localizedDescription)
                }
            })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 2022-08-07
      • 1970-01-01
      • 2013-03-25
      • 2020-12-23
      • 1970-01-01
      相关资源
      最近更新 更多