free geoip
67

Fix Firebase Email Verification Kotlin

If you’re struggling with Firebase email verification not working in Kotlin, you’re not alone. This issue is commonly caused by…

If you’re struggling with Firebase email verification not working in Kotlin, you’re not alone. This issue is commonly caused by improper configuration in the Firebase console, not sending the verification link properly, or incorrectly handling user authentication state.

Firebase Email Verification Kotlin

This guide walks you through a complete and tested solution to implement Firebase email verification in a Kotlin-based Android app. You’ll learn how to configure Firebase correctly, send the verification email, and verify the email link status before allowing the user to proceed.

Step 1: Add Firebase to Your Android Project

Ensure Firebase is set up correctly in your app. Use the official Firebase setup guide to link your project.

Step 2: Create Your Activity Classes

1. LoginActivity.kt

class LoginActivity : AppCompatActivity() {
    private lateinit var auth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        auth = FirebaseAuth.getInstance()

        val loginButton = findViewById<Button>(R.id.btnLogin)
        loginButton.setOnClickListener {
            val email = findViewById<EditText>(R.id.etEmail).text.toString()
            val password = findViewById<EditText>(R.id.etPassword).text.toString()

            auth.signInWithEmailAndPassword(email, password).addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    val user = auth.currentUser
                    if (user != null && user.isEmailVerified) {
                        startActivity(Intent(this, HomeActivity::class.java))
                    } else {
                        Toast.makeText(this, "Please verify your email", Toast.LENGTH_LONG).show()
                    }
                } else {
                    Toast.makeText(this, "Login Failed", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}

2. RegisterActivity.kt

class RegisterActivity : AppCompatActivity() {
    private lateinit var auth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)

        auth = FirebaseAuth.getInstance()

        val registerButton = findViewById<Button>(R.id.btnRegister)
        registerButton.setOnClickListener {
            val email = findViewById<EditText>(R.id.etEmail).text.toString()
            val password = findViewById<EditText>(R.id.etPassword).text.toString()

            auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    val user = auth.currentUser
                    user?.sendEmailVerification()?.addOnCompleteListener { verifyTask ->
                        if (verifyTask.isSuccessful) {
                            Toast.makeText(this, "Verification email sent", Toast.LENGTH_LONG).show()
                        } else {
                            Toast.makeText(this, "Failed to send verification", Toast.LENGTH_SHORT).show()
                        }
                    }
                } else {
                    Toast.makeText(this, "Registration Failed", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}

Step 3: Handle Verification State on App Launch

3. SplashActivity.kt

class SplashActivity : AppCompatActivity() {
    private lateinit var auth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        auth = FirebaseAuth.getInstance()
        val user = auth.currentUser

        if (user != null && user.isEmailVerified) {
            startActivity(Intent(this, HomeActivity::class.java))
        } else {
            startActivity(Intent(this, LoginActivity::class.java))
        }
        finish()
    }
}

Step 4: Common Mistakes to Avoid

  • Not calling sendEmailVerification() after user creation.
  • Forgetting to check user.isEmailVerified before allowing login.
  • Not refreshing user state using user.reload() if needed.

By following this guide, your Firebase email verification should now work seamlessly in your Kotlin app. Make sure to test on real devices and review Firebase Authentication settings thoroughly.

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *