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

Firebase Authentication(Kotlin)邮箱更新功能异常排查

Firebase Auth邮箱更新异常解决

问题场景

实现应用内用户邮箱更新功能,基于Firebase Authentication编写了Kotlin代码,但调试时触发异常。

报错信息

The given sign-in provider is disabled for this Firebase project. Enable it in the Firebase console, under the sign-in method tab of the Auth section. [ Please verify the new email before changing email.]

(注:报错前半段为误导性提示,真实原因是更改邮箱前需先验证新邮箱)

原代码示例

val user = firebaseUser.currentUser!!
val credential = EmailAuthProvider
    .getCredential(currentEmail, currentPassword)

user.reauthenticate(credential)
    .addOnCompleteListener {
        firebaseUser.currentUser?.updateEmail(newEmail)
            ?.addOnCompleteListener { updateEmailResult ->
                if (updateEmailResult.isSuccessful) {
                    val userRef = firestore.collection("users").document(userId)
                    userRef
                        .update("email", newEmail)
                        .addOnSuccessListener {
                            onSuccess()
                        }
                        .addOnFailureListener { e ->
                            onError(e)
                        }
                } else {
                    val exception = updateEmailResult.exception
                }
            }

解决步骤

  • 调整核心流程:Firebase要求更改邮箱前,必须先让用户验证新邮箱地址。需先向新邮箱发送验证邮件,待用户点击验证链接后,再执行邮箱更新操作。
  • 修改代码逻辑:
    val user = firebaseUser.currentUser!!
    val credential = EmailAuthProvider.getCredential(currentEmail, currentPassword)
    
    user.reauthenticate(credential)
        .addOnCompleteListener { reauthTask ->
            if (reauthTask.isSuccessful) {
                // 先发送新邮箱验证邮件
                user.sendEmailVerification()
                    .addOnCompleteListener { verifyTask ->
                        if (verifyTask.isSuccessful) {
                            // 提示用户前往新邮箱点击验证链接,验证完成后执行更新
                            user.updateEmail(newEmail)
                                .addOnCompleteListener { updateTask ->
                                    if (updateTask.isSuccessful) {
                                        // 同步更新Firestore中的邮箱字段
                                        firestore.collection("users").document(userId)
                                            .update("email", newEmail)
                                            .addOnSuccessListener { onSuccess() }
                                            .addOnFailureListener { e -> onError(e) }
                                    } else {
                                        onError(updateTask.exception!!)
                                    }
                                }
                        } else {
                            onError(verifyTask.exception!!)
                        }
                    }
            } else {
                onError(reauthTask.exception!!)
            }
        }
    
  • 确认控制台设置:进入Firebase控制台→认证→登录方法,确认“邮箱/密码”登录提供商处于启用状态,排除其他潜在问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 07:48:30