Email Sign Up (Firebase) - Kotlin

 


             Implementing Email Sign Up using firebase


  • Add this dependency to build.gradle.kts.

implementation("com.google.firebase:firebase-auth:22.3.0") implementation("com.google.firebase:firebase-database:20.3.0")

RegisterActivity

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.google.firebase.database.FirebaseDatabase import com.rrmchathura.realestate.MyUtils import com.rrmchathura.realestate.databinding.ActivityRegisterEmailBinding class RegisterEmailActivity : AppCompatActivity() { private lateinit var binding:ActivityRegisterEmailBinding private val TAG = "REGISTER_EMAIL_TAG" private lateinit var firebaseAuth : FirebaseAuth private lateinit var progressDialog: ProgressDialog override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityRegisterEmailBinding.inflate(layoutInflater) setContentView(binding.root) progressDialog = ProgressDialog(this) progressDialog.setTitle("Please wait") progressDialog.setCanceledOnTouchOutside(false) firebaseAuth = FirebaseAuth.getInstance() binding.toolbarBackBtn.setOnClickListener { finish() } binding.haveAccountTv.setOnClickListener { finish() } binding.registerBtn.setOnClickListener { validateData() } } private var email = "" private var password = "" private var cPassword = "" private fun validateData(){ email = binding.emailEt.text.toString().trim() password = binding.passwordEt.text.toString() cPassword = binding.cPasswordEt.text.toString() Log.d(TAG,"validateData: Email $email") Log.d(TAG,"validateData: Password $password") Log.d(TAG,"validateData: cPassword $cPassword") 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 if (cPassword.isEmpty()){ binding.cPasswordEt.error = "Confirm password" binding.cPasswordEt.requestFocus() } else if (password != cPassword){ binding.cPasswordEt.error = "Password doesn't match" binding.cPasswordEt.requestFocus() } else{ registerUser() } } private fun registerUser(){ progressDialog.setMessage("Creating Account") progressDialog.show() firebaseAuth.createUserWithEmailAndPassword(email,password).addOnSuccessListener { Log.e(TAG,"registerUser: Register Success") updateUserInfo() } .addOnFailureListener { e-> Log.e(TAG,"registerUser: ${e.message}") MyUtils.toast(this,"Failed due to ${e.message}") progressDialog.dismiss() } } private fun updateUserInfo(){ progressDialog.setMessage("Saving User Info.....") progressDialog.show() val timestamp = MyUtils.timestamp() val registeredUserEmail = firebaseAuth.currentUser!!.email val registeredUserUid = firebaseAuth.uid val hashMap = HashMap() hashMap["uid"] = "$registeredUserUid" hashMap["email"] = "$registeredUserEmail" hashMap["name"] = "" hashMap["timestamp"] = timestamp hashMap["phoneCode"] = "" hashMap["phoneNumber"] = "" hashMap["dob"] = "" hashMap["userType"] = MyUtils.USER_TYPE_EMAIL hashMap["token"] = "" val ref = FirebaseDatabase.getInstance().getReference("Users") ref.child("$registeredUserUid").setValue(hashMap).addOnSuccessListener { Log.e(TAG,"updateUserInfo: Info saved") progressDialog.dismiss() startActivity(Intent(this,MainActivity::class.java)) finishAffinity() } .addOnFailureListener {e-> Log.e(TAG,"updateUserInfo: ${e.message}") MyUtils.toast(this,"Failed due to ${e.message}") progressDialog.dismiss() } } }

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