- Home
- Services
- IVY
- Portfolio
- Blogs
- About Us
- Contact Us
- Sun-Tue (9:00 am-7.00 pm)
- infoaploxn@gmail.com
- +91 656 786 53
Integrating social login is a crucial step in modern mobile application development. It improves user experience by offering a seamless and quick way to sign in. In this post, I’ll walk you through the complete process of setting up social login (Google Sign-In) using Firebase Authentication in a Flutter application.
I’ll cover everything from Firebase project configuration to writing the Dart code that enables users to sign in with their Google accounts. This guide assumes you already have a basic Flutter project set up.
To start with, I created a new Firebase project:
Here, I needed to provide:
If targeting iOS, I would follow a similar process, but download the GoogleService-Info.plist file and place it in the iOS project root.
In the Flutter project, I first added the necessary Firebase dependencies in pubspec.yaml:
dependencies: flutter: sdk: flutter firebase_core: ^2.24.0 firebase_auth: ^4.15.0 googlesignin: ^6.1.5
After running flutter pub get, I initialized Firebase in the main.dart file:
void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(MyApp()); }
This step ensures that Firebase is initialized before the app starts.
Next, I needed to enable Google Sign-In in Firebase Authentication:
For Android, Firebase requires an SHA-1 key. I generated it using the command below:
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
I added this SHA-1 key in the Firebase project settings under my registered Android app. This step is critical for Google Sign-In to work properly.
Now it was time to write the code that handles Google Sign-In. I created an authentication service:
import ’package:firebaseauth/firebaseauth.dart’; import ’package:googlesignin/googlesignin.dart’; class AuthService { final FirebaseAuth _auth = FirebaseAuth.instance; final GoogleSignIn _googleSignIn = GoogleSignIn(); Future<User?> signInWithGoogle() async { try { final GoogleSignInAccount? googleUser = await _googleSignIn.signIn(); if (googleUser == null) { return null; // User cancelled the sign-in } final GoogleSignInAuthentication googleAuth = await googleUser.authentication; final credential = GoogleAuthProvider.credential( accessToken: googleAuth.accessToken, idToken: googleAuth.idToken, ); final userCredential = await _auth.signInWithCredential(credential); return userCredential.user; } catch (e) { print(’Google Sign-In error: $e’); return null; } } Future<void> signOut() async { await _googleSignIn.signOut(); await _auth.signOut(); } }
This class handles the complete flow of signing in and out using Google credentials.
In my Flutter app, I created a button that users can tap to log in:
ElevatedButton( onPressed: () async { final user = await AuthService().signInWithGoogle(); if (user != null) { print("Signed in: ${user.displayName}"); } else { print("Sign-In cancelled or failed."); } }, child: Text("Sign in with Google"), )
I also added a sign-out button for good measure:
ElevatedButton( onPressed: () async { await AuthService().signOut(); print("Signed out successfully."); }, child: Text("Sign Out"), )
Although this post focuses on Google Sign-In, Firebase also supports other providers like Facebook, Twitter, GitHub, and Apple. Each of them has its own setup steps in the Firebase console, including client IDs and secret tokens.
For example, enabling Facebook login would require:
Apple Sign-In, especially for iOS apps, is also mandatory if you offer any third-party sign-in options on Apple devices.
Although this post focuses on Google Sign-In, Firebase also supports other providers like Facebook, Twitter, GitHub, and Apple. Each of them has its own setup steps in the Firebase console, including client IDs and secret tokens.
For example, enabling Facebook login would require:
Apple Sign-In, especially for iOS apps, is also mandatory if you offer any third-party sign-in options on Apple devices
Imagine reducing your operational costs by up to $100,000 annually without compromising on the technology you rely on. Through our partnerships with leading cloud and technology providers like AWS (Amazon Web Services), Google Cloud Platform (GCP), Microsoft Azure, and Nvidia Inception, we can help you secure up to $25,000 in credits over two years (subject to approval).
These credits can cover essential server fees and offer additional perks, such as:
By leveraging these credits, you can significantly optimize your operational expenses. Whether you're a startup or a growing business, the savings from these partnerships ranging from $5,000 to $100,000 annually can make a huge difference in scaling your business efficiently.
The approval process requires company registration and meeting specific requirements, but we provide full support to guide you through every step. Start saving on your cloud infrastructure today and unlock the full potential of your business.