3 Comments on "Firebase Authentication Example in Kotlin"


  1. I was doing your course and it works.
    I’ve done exactly the same thing in another project but I add a variable of birthday and it doesn’t work anymore. The debug show me an error at the reference of the databases.
    Here is my code:

    @file:Suppress(“DEPRECATION”, “UnusedImport”)

    package com.example.france_app

    import android.app.ProgressDialog
    import android.content.Intent
    import android.os.Bundle
    import android.text.TextUtils
    import android.util.Log
    import android.view.View
    import android.widget.Button
    import android.widget.EditText
    import android.widget.Toast
    import com.google.firebase.database.IgnoreExtraProperties
    import androidx.appcompat.app.AppCompatActivity
    import com.google.firebase.auth.FirebaseAuth
    import com.google.firebase.database.DatabaseReference
    import com.google.firebase.database.FirebaseDatabase

    @Suppress(“DEPRECATION”)
    class RegistrationActivity : AppCompatActivity() {

    private var etFirstName: EditText? = null
    private var etLastName: EditText? = null
    private var etEmail: EditText? = null
    private var etPassword: EditText? = null
    private var etDateNaissance : EditText? = null
    private var btnCreateAccount: Button? = null
    private var mProgressBar: ProgressDialog? = null

    //Firebase references
    private var mDatabaseReference: DatabaseReference? = null
    private var mDatabase: FirebaseDatabase? = null
    private var mAuth: FirebaseDatabase? = null

    private val TAG = “RegistrationActivity”

    //Global variables
    private var firstName: String? = null
    private var lastName: String? = null
    private var email: String? = null
    private var password: String? = null
    private var dateNaissance: String? = null

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

    //Initialise the references of Firebase database
    initialise()
    }

    //Function who initalise this references
    private fun initialise() {
    etFirstName = findViewById(R.id.signUp_nom) as EditText
    etLastName = findViewById(R.id.signUp_prenom) as EditText
    etEmail = findViewById(R.id.signUp_email) as EditText
    etPassword = findViewById(R.id.signUp_email) as EditText
    etDateNaissance = findViewById(R.id.signUp_dateNaissance) as EditText
    btnCreateAccount = findViewById(R.id.validateRegistration) as Button
    mProgressBar = ProgressDialog(this)

    mDatabase = FirebaseDatabase.getInstance()
    mDatabaseReference?.child(“Users”)
    mDatabaseReference = mDatabase?.reference?.child(“Users”)

    btnCreateAccount!!.setOnClickListener {
    createNewAccount()
    }
    }

    private fun createNewAccount() {

    val mAuth = FirebaseAuth.getInstance()

    //We get current string values present in EditText gaps
    firstName = etFirstName?.text.toString()
    lastName = etLastName?.text.toString()
    email = etEmail?.text.toString()
    password = etPassword?.text.toString()
    dateNaissance = etDateNaissance?.text.toString()

    //if there is text in the gaps
    if (!TextUtils.isEmpty(firstName) && !TextUtils.isEmpty(lastName)
    && !TextUtils.isEmpty(email) && !TextUtils.isEmpty(password) &&
    TextUtils.isEmpty(dateNaissance))
    {
    //we create a new user
    val mAuth = FirebaseAuth.getInstance()
    mAuth
    .createUserWithEmailAndPassword(email!!, password!!)
    .addOnCompleteListener(this) { task ->
    mProgressBar!!.hide()

    if (task.isSuccessful) {
    //Sign in success, Update Ui with the signed-in user’s information
    Log.d(TAG, “createUserWithEmail:success”)
    val userId = mAuth!!.currentUser!!.uid

    //Verify Email
    verifyEmail()

    //Update user profile information
    val currentUserDb = mDatabaseReference!!.child(userId)
    currentUserDb.child(“firstName”).setValue(firstName)
    currentUserDb.child(“lastName”).setValue(lastName)
    currentUserDb.child(“email”).setValue(email)
    currentUserDb.child(“password”).setValue(password)
    currentUserDb.child(“dateNaissance”).setValue(dateNaissance)

    updateUserInfoAndUI()
    } else {
    //If sign in fails, display a message to the user
    Log.w(TAG, “createUserWithEmail:failure”, task.exception)
    Toast.makeText(this, “Authentication failed”, Toast.LENGTH_SHORT).show()

    }
    }

    //We show the progress bar while the registration is done
    mProgressBar!!.setMessage(“Enregistrement du User”)
    mProgressBar!!.show()

    } else {
    Toast.makeText(this, “Veuillez entrer tout les détails”, Toast.LENGTH_SHORT).show()
    }
    }

    private fun updateUserInfoAndUI() {
    val intent = Intent(this, Home_Activity::class.java)
    //The FLAG_ACTIVITY_CLEAR_TOP clear the precedent activity so that if
    // user press back from the next activity, he should not be taken back
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    startActivity(intent)
    }

    private fun verifyEmail() {
    val mAuth = FirebaseAuth.getInstance()
    val mUser = mAuth!!.currentUser
    mUser!!.sendEmailVerification()
    .addOnCompleteListener(this) { task ->

    if (task.isSuccessful) {
    Toast.makeText(
    this,
    “Verification Email sent to ” + mUser.email,
    Toast.LENGTH_SHORT
    ).show()
    } else {
    Log.e(TAG, “sendEmailVerification”, task.exception)
    Toast.makeText(this, “Failed to end verification email”, Toast.LENGTH_SHORT)
    .show()
    }
    }
    }
    }

    Reply

  2. Actually this blog covered everything I wanted to learn concerning firebase Auth… Thanks a lot Sergey !

    private fun youReallyHelpedMe(){
    val iAmGrateful: String = “100%”
    }

    Reply

Leave a Reply

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

Free Video Lessons

Enter your email and stay on top of things,

Subscribe!