Today we are going to deal with Firebase Real-time database in Kotlin. If you did not already know how to add firebase to your android project you may follow instructions on official Firebase Setup Guide. But the easiest way is to add firebase with the help of Android Studio. You may choose any method that you like and feel comfortable with. We are not going to deal with the setup task or GUI development related tasks. We will only focus on Kotlin functions that could be used for handling the Firebase Real-time Database.
Login Task
First of all, we need to make a Login and Registration form for firebase applications. Although it is not necessary as we can use the database in test mode where we could use a database without authentication. But we highly recommend not doing this and make a simple registration and Login form as this is very easy to do this. Here is a link for Easy Firebase Authentication in Kotlin example. You may also do this from scratch if you feel comfortable with this. But if you really need to do this with all the required functionalities like Email confirmation and password reset and other Detail Registration-related task then you can follow this Firebase Authentication Example in Kotlin. With all the above-mentioned references, we are also going to explain here with short code snippets to make things simple and easy to remember.
Auth Reference
First of all you need to save Auth reference to do Authentication related tasks which include sign-in, sign-up, and sign-out tasks. You can declare a late init variable like this
private lateinit var mAuth: FirebaseAuth
Code language: PHP (php)
and then you can initialize this in onCreate method like this
mAuth = FirebaseAuth.getInstance();
If you need to check whether the user already logged in then you can simply check if the Auth user is null or not. You can do something like this if you also want to go to dashboard activity if the user is already logged in.
if(mAuth.currentUser!=null){
val i = Intent(baseContext, DashboardActivity::class.java)
startActivity(i);
finish()
}
Code language: JavaScript (javascript)
Sing In User with Email and Password
mAuth.signInWithEmailAndPassword(usrEmailId,usrPassword)
.addOnCompleteListener {task ->
if(task.isSuccessful){
}else{
}
}
Code language: JavaScript (javascript)
Registering a new user with Email and password
mAuth.createUserWithEmailAndPassword(usrEmailId, usrPassword)
.addOnCompleteListener {task ->
if(task.isSuccessful){
}else{
}
}
Code language: JavaScript (javascript)
Log-Out User
FirebaseAuth.getInstance().signOut()
this.finish()
Code language: CSS (css)
Also make sure to Enable Email/Password Sign-in Provider from Sign-in Method menu like shown below.
Database Tasks
Now that we are done with our sign-in Tasks, we are good to go to our Firebase Realtime database related tasks. Because it is a database so we need to do CRUD operations on it. But it is No-SQL based database so we also need to take care of methods to read and writings of Objects to the database.
Create/Update simple variable to the Database
First of all Let’s write a simple variable into our database under our root reference. Or its like global variable which is not nested into any key value parent. One more thing to be mention here is that values are stored in key-value pair like manner. Let’s suppose we want to save “student_id = 10001” into our database so we can do this using following code
Let’s take Database reference first just like we did while doing authentication with the Firebase.
private val myRef = FirebaseDatabase.getInstance().reference
Code language: PHP (php)
with the help of above reference variable you can create or update value like this
myRef.child("student_id").setValue(10001)
Code language: CSS (css)
Please note that this will automatically create a new child if there is no child previously there with key “student_id” there. If there is already a child present then it will just update its value. you can also use any data-type here. Like Double, Integer, Float, or String, etc.
Remove Value from Firebase Database
It is very easy to remove a child from Firebase Database. All you have to do is to call removeValue() method. It’s just as simple as that
FirebaseDatabase.getInstance().reference.child("student_id").removeValue()
Code language: CSS (css)
Or if you want to use above reference variable then the code will be like this
myRef.child("student_id").removeValue()
Code language: CSS (css)