【问题标题】:How to change email address in Firebase?如何在 Firebase 中更改电子邮件地址?
【发布时间】:2020-08-25 09:53:19
【问题描述】:

我在 Firebase 身份验证中更改电子邮件地址时遇到了一些问题。 我的代码现在看起来像这样:

func changeEmail(withEmail email: String, completion: @escaping ((Bool) -> Void)) {
    guard let currentUser = Auth.auth().currentUser, let email = mail else { return }
    currentUser.updateEmail(to: email) { [weak self]
        error in
        guard let self = self else { return }
        let title: String
        let message: String
        if let error = error {
            title = "alert.error.title".localized()
            message = error.localizedDescription
        } else {
            title = email
            message = "auth.confirm.email.popup".localized()
            currentUser.sendEmailVerification()
        }
        self.navigator.showAlert(title: title,
                                 message: message,
                                 bottomLeftTitle: "general.got.it".localized(),
                                 bottomLeftHandler: { completion(error == nil)
        })
    }
}

所以没关系,并且可以正常工作,并且用户实际上可以更改电子邮件。

但是当用户停留时间过长需要重新登录时会出现问题。每个人都知道这会扰乱应用中的用户体验。

Auth.auth().reload() //not working in this situation.

那么如何在不要求用户注销并重新登录的情况下更改电子邮件?

【问题讨论】:

    标签: swift firebase firebase-authentication


    【解决方案1】:

    有一个reauthenticate 方法正是为此目的。 https://firebase.google.com/docs/auth/ios/manage-users#re-authenticate_a_user

    您需要做的是再次向用户询问其登录凭据。无需注销 - 需要登录。

    可能的代码:

    if (self.newPassword == self.newPasswordConfirm) && (!(self.newPassword.isEmpty) || !(self.newUserName.isEmpty)) {
        reauthenticate(email: self.accountEmail, password: self.oldPassword) { isSucceeded in
            //Successfully authenticated
            if isSucceeded == true {
                if !self.newUserName.isEmpty {
                    // update username
                }
                
                Auth.auth().currentUser?.updatePassword(to: self.newPassword) { (error) in
                    // Alert user that it didn't work
                }
                
                self.editProfile.toggle()
            }
            // Failed to reauthenticate
            else if isSucceeded == false {
                // Alert User
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-16
      • 2016-10-14
      • 1970-01-01
      • 2016-03-06
      • 2019-01-11
      • 1970-01-01
      • 2011-09-09
      • 2011-04-15
      相关资源
      最近更新 更多