Skip to main content

How to implement Google sign-in with Flutter in 10 minutes!

Today I am discussing how to authenticate your Flutter app using 'flutter firebase auth', specifically, Google sign-in with Flutter because it is the most commonly used firebase_auth method. 

First, lets start with adding required dependencies to your project. 


Firebase flutter 0



Note: Make sure you are installing the latest 'firebase_auth' and 'google_sign_in' libraries. 

Now, we are going to configure the Firebase console. If you haven’t set up your Firebase account yet, log in to Firebase with your Gmail account, and create a project in the console. 


                                  firebase_auth console

                                                                                                   Firebase console


Now you need to go to the authentication tab as shown in the above image. Enable Google authentication from the Sign-in provider’s list. Then you will be asked to follow some steps to enable authentication. Follow those instructions.

Now let’s start implementing firebase_auth in our application.


Import 'firebase_auth' and 'google_sign_in', and create instances


Firebase flutter 1



method

Below code will authenticate your user and return the current user.


Firebase flutter 3



Now, lets implement the sign out method


Firebase flutter 4



Now, we have implemented functions to sign-in and sign-out with Google authentication. However there is no need for the user to log-in every time the application is open. Therefore we have to modify this to address the currently logged-in user.


Firebase flutter 5



With the above code, you can access the currently logged-in user at the splash screen in your app. Then you can decide to display either the login UI or the main screen. Now we have all the functions that we need to authenticate the user.


This is where you can decide whether to display the login screen or not.


Firebase flutter 6



Now, I am going to implement a sample log-in screen with 'google sign-in flutter'.


Firebase flutter 7


This is all you have to do to implement 'firebase auth' in your Flutter app. I hope you enjoyed this coding tutorial! If there are any questions, use the comments section below. 



© 2021 Creative Software. All Rights Reserved | Privacy | Terms of Use