Firebase authentication – Check user logged in and delay

In the last chepters of the Firebase authentication Android tutorial we have talked about how can we implement the Firebase registration in the app using email. Then in the very last chapter we have refactored the code to use Koin. Before the implementation of the login part using email and password, we gonna add some code to the SplashActivity::class.
When the user opens the app, then in this activity we have to check if the user logged in. Next to this we gonna have a few seconds delay to show longer the Splash screen for the user.

GitHub

If you haven’t done the previous chapters, then from GitHub you can download the starter project for this chapter.

GitHub – add_koin branch

Step 1 – Extend UserRepository::class

In the chapter of Register with email using coroutine we have created the needed classes for the Firebase’s and Firestore’s queries. We have created for them first an interface, which will contain all the needed functions for the registration and login.

In this chapter we are going to extend it to fetch the currently logged in user from Firebase. This method will give back a FirebaseUser object.

So open the UserRepository::class from the “repository” package and add this line of code to it.

suspend fun checkUserLoggedIn(): FirebaseUser?

Note that we have started again this function whit the keyword of “suspend”.

Suspending functions are at the center of everything coroutines. A suspending function is simply a function that can be paused and resumed at a later time. They can execute a long running operation and wait for it to complete without blocking.

The syntax of a suspending function is similar to that of a regular function except for the addition of the suspend keyword. It can take a parameter and have a return type. However, suspending functions can only be invoked by another suspending function or within a coroutine.

That’s all. Now you can close this file.

Step 2 – UserRepositoryImpl::class

Now open the UserRepositoryImpl::class. Because you have added a new function to the UserRepository::class, you should see a red underline at the beginning of this class. It is there, because you have to implement this method here also.

We gonna learn a new way of creating overridable methods. So in Android Studio press Control+o. In the popup window you should have the method of checkUserLoggedIn(). Select it and click on the “Ok” button.

Inside the body of this function we are going to have only one line of code. This will get back for us the logged in user from the Firebase’s authentication. For this we can use the variable of “firebaseAuth”. So add this line there.

return firebaseAuth.currentUser

Step 3 – FirebaseViewModel

In the FirebaseViewModel::class we are going to call the previously created checkUserLoggedIn() method. In the chapter of the registration we were talking about that the ViewModel class has an own CoroutineScope, called viewModelScope. In this scope we can call suspended functions, just like the checkUserLoggedIn() method is.

Because of the CoroutineScope, we have to create a temporary instance about the FirebaseUser::class, and this method will return this instance.

After this short summary our function should look like this:


checkUserLoggedIn

Step 4 – SplashActivity

After the implementations of these methods finally we can add the code to the SplashActivity::class.

Add member variables

In this activity we are going to have 3 member variables. One for the FirebaseViewModel::class, one for the FirebaseUser::class and one for a new CoroutineScope, which will be coupled to this activity.

So just add this lines to the activity.


Member variables of the SplashActivity::class
Note that we are going to use the Main Dispatcher.

A coroutine dispatcher that is confined to the Main thread operating with UI objects. Usually such dispatchers are single-threaded.

In our case the users should wait until the check of there status will be finished.

Launch the CoroutineScope

Next is to launch the CoroutineScope inside of the onCreate() method. After the “setContentView” call add these lines.

coroutineScope.launch {
}

Then inside of the CoroutineScope first we are going to initialize the “currentFirebaseUser” variable using the checkUserLoggedIn() method, what we can reach on the variable of “firebaseViewModel”.

Furthermore, we will have an if-else-statement, because we have to make sure the user is logged in.

If the currentFirebaseUser variable equals to null, then we are going to start the RegisterActivity::class. If it is not null, then we gonna launch the MainActivity::class.

So add this if-else-statement inside of the coroutineScope.

if (currentFirebaseUser == null)
{

}
else
{

}

startActivity(activity: Activity)

We are going to add separetly a function for the Intent to start the activities. So our code won’t be boilerplate. The name of this function will be startActivity. It will have only one argument. This will be the activity, what we want to start.

Furthermore, we want to prevent the backwards navigation to the SplashActivity, so we are going to clear the back stack using a flag called “FLAG_ACTIVITY_CLEAR_TASK” and we gonna start the new activity with the flag of “FLAG_ACTIVITY_NEW_TASK”.

So here is the code.


startActivity()
if (currentFirebaseUser == null)

As we have talked about, in case if the currentFirebaseUser equals to null, then we are going to start the RegisterActivity::class. Hence add this line to this part of the if-else-statement.

startActivity(RegisterActivity())

The else statement

In the else part we are going to use the currentFirebaseUser varaible safely. So on the variable we are going to call the .let lambda then we will start the MainActivity::class.

startActivity(MainActivity())

The delay

Delay doesn’t block a thread, but only suspends the coroutine itself. The thread is returned to the pool while the coroutine is waiting, and when the waiting is done, the coroutine resumes on a free thread in the pool. The main thread must wait until our coroutine completes.

Add this line of code before the previous if-else-statement, but still inside of the “coroutineScope.launch”.

delay(3_000)

Finally the whole code of the coroutineScope:


coroutineScope

Run the app

If you run now the app, then it should show you first the SplashActivity::class and after the delay it will be redirected to the next activity.

GitHub

The source code is available on GitHub!

GitHub – check_user_logged_in_delay branch

Questions

I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂

 


 

Follow and like us:
Click to rate this post!
[Total: 0 Average: 0]

Leave a Reply

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