【问题标题】:Firebase isEmailVerify not working in KotlinFirebase isEmailVerify 在 Kotlin 中不起作用
【发布时间】:2020-01-06 04:51:58
【问题描述】:

注意:我能够弄清楚这一点。无需更改 Firebase 中的规则。请参阅下面的代码。

原帖 我有一个 IOS 应用程序,我决定构建 Android/Kotlin 版本,但我很难使用 Firebase/isEmailVerify。我可以注册一个新用户并发送电子邮件进行验证,但是,如果我不验证,我仍然可以登录。我是 Kotlin 的新手。任何帮助是极大的赞赏。

更新代码

class LoginActivity : AppCompatActivity() {

lateinit var auth: FirebaseAuth
private var emailVerifier: Boolean = true

private val emailVerificationAlert = { _: DialogInterface, _: Int ->
    Toast.makeText(this.applicationContext, android.R.string.yes, Toast.LENGTH_SHORT).show()
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)
    auth = FirebaseAuth.getInstance()
}

private fun verifyEmail() {
    val user = FirebaseAuth.getInstance().currentUser
    if (user != null) {
        emailVerifier = user.isEmailVerified()
    }
    if (emailVerifier) {
        finish()
    } else {
        userDidNotVerify()
        auth.signOut()
    }
}

fun loginBtnClicked(view: View) {
    val email = loginEmailTxt.text.toString()
    val password = loginPasswordTxt.text.toString()

    auth.signInWithEmailAndPassword(email, password)
        .addOnSuccessListener { exception ->
            println("USER LOGGED IN")
            verifyEmail()
        }
        .addOnFailureListener { exception ->
            Log.e("Exception", "Could not sign in user - ${exception.localizedMessage}") 
    }
}

private fun userDidNotVerify() {
    val builder = android.app.AlertDialog.Builder(this)
    with(builder) {
        this.setTitle("Confirm your email address.")
        this.setMessage("A confirmation email has been sent to" + " " + (loginEmailTxt.text) + " " +
                    "." + " " + "Click on the confirmation link to activate your account")
        this.setPositiveButton("OK", DialogInterface.OnClickListener(function = emailVerificationAlert))
        this.show()
    }
}

fun loginCreateClicked(view: View) {
    val createIntent = Intent(this, CreateUserActivity::class.java)
    startActivity(createIntent)
}

}

【问题讨论】:

    标签: android firebase kotlin firebase-authentication


    【解决方案1】:

    预计用户仍然可以在电子邮件通过验证之前登录。这为您的应用提供了一种方法,允许用户请求发送另一封验证电子邮件,以防第一封邮件发生问题。

    如果您想在验证电子邮件之前限制用户可以执行的操作,您可以检查 UserInfo 对象上的isEmailVerfied(),您还可以使用安全规则中的auth.token.email_verified 来限制他们对数据库和存储的访问权限由 Firebase 提供。

    【讨论】:

    • 您好,Doug,我能够在 IOS 中使用它,而无需修改 Firebase 中的规则。如果用户不验证电子邮件,他们就不会去任何地方。我只是想找到在 Kotlin 中完成这项工作的方法。这是有关该应用程序的IOS版本的帖子。 stackoverflow.com/a/59607015/11151290 谢谢。
    • 你在那里使用self.authUser!.isEmailVerified。在此处的答案中,我为您提供了指向 Java/Kotlin 等效调用的链接。您可以自行编写应用逻辑以按照自己的方式使用它。
    猜你喜欢
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多