Email Sign In (Firebase) - Kotlin


 

            Implementing Email Sign In using firebase


  • Add this dependency to build.gradle.kts.
implementation("com.google.firebase:firebase-auth:22.3.0")

LoginActivity

package com.rrmchathura.realestate.activities import android.app.ProgressDialog import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.util.Patterns import com.google.firebase.auth.FirebaseAuth import com.rrmchathura.realestate.MyUtils import com.rrmchathura.realestate.databinding.ActivityLoginEmailBinding class LoginEmailActivity : AppCompatActivity() { private lateinit var binding: ActivityLoginEmailBinding private val TAG = "LOGIN_EMAIL_TAG" private lateinit var progressDialog: ProgressDialog private lateinit var firebaseAuth: FirebaseAuth override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityLoginEmailBinding.inflate(layoutInflater) setContentView(binding.root) progressDialog = ProgressDialog(this) progressDialog.setTitle("Please wait") progressDialog.setCanceledOnTouchOutside(false) firebaseAuth = FirebaseAuth.getInstance() binding.toolbarBackBtn.setOnClickListener { finish() } binding.loginBtn.setOnClickListener { validateData() } binding.noAccountTv.setOnClickListener { startActivity(Intent(this,RegisterEmailActivity::class.java)) } } private var email = "" private var password = "" private fun validateData() { email = binding.emailEt.text.toString().trim() password = binding.passwordEt.text.toString() Log.d(TAG, "validateData: Email: $email") Log.d(TAG, "validateData: Password: $password") if (email.isEmpty()) { binding.emailEt.error = "Enter Email" binding.emailEt.requestFocus() } else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) { binding.emailEt.error = "Invalid Email" binding.emailEt.requestFocus() } else if (password.isEmpty()) { binding.passwordEt.error = "Enter Password" binding.passwordEt.requestFocus() } else { loginUser() } } private fun loginUser() { progressDialog.setMessage("Logging In.....") progressDialog.show() firebaseAuth.signInWithEmailAndPassword(email, password).addOnSuccessListener { Log.d(TAG, "loginUser: Logged In...") progressDialog.dismiss() startActivity(Intent(this, MainActivity::class.java)) finishAffinity() } .addOnFailureListener { e -> Log.d(TAG, "loginUser: ${e.message}") progressDialog.dismiss() MyUtils.toast(this, "Failed due to ${e.message}") } } }

Post a Comment

0 Comments

Youtube Channel Image
Coding With Chathura Subscribe To watch more Project Tutorials
Subscribe
Do you have any doubts? chat with us on WhatsApp
Hello, How can I help you? ...
Click me to start the chat...

Welcome to freesourcecodelk

X