Firebase Authentication with GetX in Flutter
Tags: flutter, firebase, authentication
Published: 2021-10-16 15:53:21

#Introduction 🎉
You know about GetX. It is an extra-light and robust solution for Flutter. It combines high-performance state management, intelligent dependency injection, and route management quickly and practically.

<br />
![I won](https://miro.medium.com/max/498/1*ttg9ZoWbDPCV74ESDvmOHw.gif)
<br />


I’m going to be honest; I’m not that pro at using GetX. So one day, I wanted to use GetX to make a flutter app with firebase authentication to understand better, but I was confused. Then it took me some time to figure out how it was working, and eventually, I succeeded and fell in love with the world of GetX.


<br />
![I won](https://miro.medium.com/max/500/1*UD8yxXPlM3xhZaumoBtz7A.gif)
<br />


#Let’s Get Started 🎯
I will assume that you have a starter template setup to enter email, password, or a button to login with Gmail. Let’s list the steps necessary to make the authentication process.
<br />
<br />

#Steps 👣

###1. Configuring the Flutter Project
###2. Connect App to Firebase
###3. Creating the Auth Controller
###4. Inject the auth controller
###5. Login or Signing up Users
###6. Success
<br />

<br />
![I won](https://miro.medium.com/max/320/1*Dk3weLuWt6CezEwKZriHdg.gif)
<br />


#1. Configuring the Flutter project ⚙️
Let’s add the necessary packages that we’re going to use throughout the application.

Copy the dependencies to your Pubspec.yaml file. I am using the latest version available now at this moment.latest version available now at this moment.

<br />
{% gist https://gist.github.com/ImranSefat/7c0c530b9b3723333f65514a2c3ececa.js %}
<br />

#2. Connect App to Firebase 🤝
Now we have to connect our app to firebase. First, go to the firebase console and create your project if you haven’t already. You will see an option to add an app to firebase. Depending on your preference and requirement, do that. The steps are pretty straightforward, and firebase will show you what to do and how.

<br />
![Firebase Console](https://miro.medium.com/max/700/1*OL30IVcxpNRQtNU0f8CX0A.png)
<br />

> Don’t forget to enable the Email/Password Sign in and Google Sign In methods.

<br />
![Sign In Method](https://miro.medium.com/max/700/1*HpZoBQBpBGWkxmkogfYHQA.png)
<br />

#3. Creating the Auth Controller 🎮
First, let’s make a constant file for the sake of simplicity and not to get confused or code duplications. Copy the below codes and paste them into your dart file inside the lib folder.
<br />

> Suggestion: Make a folder inside of the lib folder titled Constants.

<br />

{% gist https://gist.github.com/ImranSefat/578655b116bab2f557543bcb2bec4f37.js %}
<br />
<br />


Now let’s create the Auth Controller where all of our business login related to Firebase Authentication will work. Just copy the below codes and paste them into your dart file inside of your lib folder.

<br />

> Suggestion: Make a folder for titled Controller.

<br />
{% gist https://gist.github.com/ImranSefat/1a3d2f1b949e3861becac046e8541710.js %}
<br />

#4. Inject the Auth Controller 💉
We have created the Auth Controller, but how are we going to use it? Let’s hop into the main.dart file and make some changes to our code.
<br />

<br />
{% gist https://gist.github.com/ImranSefat/f83aa50e35ce6a818d965996a0ebe068.js %}
<br />

> Here, we are initializing the firebase and injecting the AuthController instance in the 4th line.


<br />
![Sign In Method](https://miro.medium.com/max/500/1*GiGyQlLWQTmmYb2EiQriwA.gif)
<br />

> Our dependencies are completed and initialized. Now let’s sign up some users.
<br />

#5. Login or Signing up Users 🙋‍♂️🙋‍♀️
I assume you have a basic or super beautiful Signup or login screen ready. If not, you can copy my code for the screen, but note that it’s a super simple screen.
<br />

<br />
{% gist  https://gist.github.com/ImranSefat/8ad729ce0df4d7c33aa34b21c4e04892.js %}
<br />

The above code is pretty self-explanatory. There are just two (2) text fields for email and password, one button to login and one to Register / Signup. The last elevated button is for the google authentication popup.

#6. Success 🎉
You just implemented Firebase Authentication in Flutter with GetX.

<br />
<br />


I have a Youtube Channel Named [“Coding with Imran”](https://www.youtube.com/c/CodingwithImran)

Do subscribe to my channel if you want to get future updates!