You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS Firebase重新认证报错:Credential未初始化就使用

Fixing "Credential used before its being initialized" Error in Firebase Auth

Hey there! Let's get that error sorted out quickly. The root cause here is simple: you've declared the credential variable but never gave it a value before trying to use it in reauthenticateAndRetrieveData(with:completion:). Swift won't let you use an uninitialized variable, hence the error message.

What You Need to Do

To fix this, you first need to create a valid AuthCredential instance based on how your user originally signed up (email/password, Google, Facebook, etc.). Then pass that initialized credential to the re-authentication method.

Here's a corrected example, assuming your user uses email/password authentication:

if error._code == 17014 { 
    // Required recent authentication 
    // First, fetch the user's current email (make sure they have one)
    guard let userEmail = Auth.auth().currentUser?.email else {
        self.ShowAlert(title: "Error", message: "Could not retrieve user email")
        return
    }
    
    // Get the user's current password (**don't hardcode this!** Use a text field input)
    guard let userPassword = passwordTextField.text, !userPassword.isEmpty else {
        self.ShowAlert(title: "Error", message: "Please enter your current password")
        return
    }
    
    // Create the email/password credential
    let credential = EmailAuthProvider.credential(withEmail: userEmail, password: userPassword)
    
    // Perform re-authentication with proper completion handling
    user.reauthenticateAndRetrieveData(with: credential) { authResult, reauthError in
        if let reauthError = reauthError {
            // Handle re-authentication failure
            self.ShowAlert(title: "Re-auth Failed", message: reauthError.localizedDescription)
        } else {
            // Re-authentication succeeded—now you can proceed with updating the email
            self.ShowAlert(title: "Success", message: "Mail Updated")
        }
    }
} else { 
    self.ShowAlert(title: "Success", message: "Mail Updated") 
}

Key Notes:

  • Never hardcode passwords: Always get the user's current password from a secure input field (like a UITextField with secure text entry enabled).
  • Other auth providers: If your user signed up with Google, Facebook, or another provider, use the corresponding credential creator. For example:
    • Google: GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)
    • Facebook: FacebookAuthProvider.credential(withAccessToken: AccessToken.current!.tokenString)
  • Handle completion callbacks: Don't pass nil as the completion handler—you need to know if re-authentication succeeded or failed before proceeding with your email update.

内容的提问来源于stack exchange,提问作者Abdelrahman Tarek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:56:37