Tech Insights
Pubudu Gamage
November 9, 2021

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

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.

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 setup your Firebase account yet, log in to Firebase with your Gmail account, and create a project in the console.


                                                                                                 Firebase console

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

Now let’s start implementing firebase_auth in our application.

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

method

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

Now,lets implement the sign out method

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 theapplication is open. Therefore we have to modify this to address the currently logged-in user.

With the above code, you can access the currently logged-in user at thesplash 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.

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

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