Google Sign-in for Flutter App on Android & Web
Firebase Authentication enables us to add various sign-in methods that include the most common federated identity providers such as Google, Facebook, Twitter, Apple etc.
Flutter Fire documentation was an excellent starting point to add Google Sign-in to my flutter app. But it was tricky to ensure the sign-in code works on Flutter Web as well. I have outlined in this article the steps to add Google Sign-in to your Flutter Mobile + Web app and some of the hurdles encountered along the way.
USE CASE
Authenticate a user by letting them sign-in using their Google account and using those credentials create a Firebase Auth User.
STEP 1: Add the app on Firebase
- Add your app on Firebase as an “android” app. Refer Add Firebase to Android documentation to do so.
- Add your app on Firebase as a “web” app. Refer Add Firebase to Web documentation to do so. (This wasn’t enough, see STEP 6)
- Additionally, you will need to add SHA-1 certificate fingerprints on your android app. Follow the below steps to achieve this;
— Execute the following command on terminal and copy the SHA-1 fingerprints value.
“keytool -list -v \ -alias androiddebugkey -keystore ~/.android/debug.keystore”
— On the Firebase console, click on “Project Overview” and click on “Settings” for your android app. Paste the copied value under “SHA certificate fingerprints”
STEP 2: pubspec.yaml changes
- Add “firebase_core”, “firebase_auth” and “google_sign_in” packages.
STEP 3: Add flutter code
- The Social Authentication example on FlutterFire was my starting point, the below “_signInUsingGoogle” method uses the credentials returned by the google auth provider, signs in the user on Firebase thus creating a Firebase User.