Perfectus: Sign-in, Firebase authentication (email/password)

Victor Apoyan
4 min readAug 10, 2020

In the last article, we learned how to set up Firebase for authentication with email and password 👇

Now it’s time for implementing authentication logic - Sign In, Sign Up, Sign Out and Reset Password in the Perfectus application using firebase auth.

Authenticator (library)

First of all, let’s define an interface which will provide authentication functionality

As you maybe noticed, I am using suspend functions

A suspending function is just a regular Kotlin function with an additional suspend modifier which indicates that the function can suspend the execution of a coroutine. Suspending functions can invoke any other regular functions, but to actually suspend the execution, it has to be another suspending function.

Read more about suspended functions 👇

Now when the interface is defined, it’s time to implement it.

As a constructor parameter, we are passing FirebaseAuth instance, which is provided by the Firebase library. FirebaseAuth instance will do all the “dirty” authentication job for us. By default, Firebase library is not providing suspending functions, to make our life easier we would use a library which will Firebase calls to suspending functions, open your build.gradle file of the module and add the dependency

you can read more about the library and implementation 👇

now it’s time to implement our first method

Sign In

here we are calling signInWithEmailAndPassword method provided by FirebaseAuth instance, which is doing authentication on the Firebase server and in the case of success returning the user object, which we are converting to IUser and returning as a result, in the case of error FirebaseAuth will throw an exception which we will catch, convert to SignInException exception and rethrow.

You maybe noticed the .await() at the end of signInWithEmailAndPassword function call, this is provided by the library with we recently added as a dependency, without that our code would look like something like this 🥶

which is quite 🤏 different from what we currently have.

Check the implementation of Sign Up, Sign Out, Reset Password and Is Signed In on the branch, feature/sign-in 👇

Authentication (module)

The authenticator library is ready and now we need a UI, so the user can input credentials and sign into the system.

Dependencies

Authentication module should depend on Authenticator library

also, it depends on Design and Logger libraries. The design library provides common colors, dimens, styles, etc, and the Logger library is used for logging.

Navigation

Architecture

In the Authentication module, an MVVM architecture pattern is used.

Creating ViewModels with Hilt is now super easy, you just need to add dependencies

After that ViewModel will look like

you just need to annotate ViewModel with @ViewModelInject, no need to create factory classes.

And this is basically it, now you can call `viewModel.signIn(email, password)` from the fragment and authenticator will do its job.

Checkout my branch and check source code on the branch feature/sign-in 👇

Let’s discuss the solution together!

You can implement Authentication flow in your application, buy just copying Authenticator library and Authentication module into your project and adding google-services.json from the Firebase Console as it was shown in my previous article 👇

I hope this was helpful.

Please Clap👏, Star 🌟my repo for upcoming features and updates and follow me 😇

--

--