Expert IT Solutions

Discover New Ideas and Solutions with CodeEssence Blogs

Get inspired with our insightful blog posts covering innovative solutions, ideas, and strategies to elevate your business.

shape image
shape image
shape image
shape image
shape image
shape image
shape image
image

Implementing Google Authentication in Flutter with Firebase

Integrating Google Authentication in your Flutter app enhances the user experience by providing a quick and secure sign-in option. This blog walks you through setting up Google Authentication in a Flutter application using Firebase. We'll cover everything from Firebase project setup to Flutter code implementation.

Step 1: Setting Up Firebase

  1. Go to the Firebase Console and create a new project.
  2. Click on 'Add app' and select the Flutter icon to add your Flutter app to the project.
  3. Follow the instructions to download the google-services.json file for Android and GoogleService-Info.plist for iOS, then place them in your app’s respective directories.

Step 2: Adding Firebase to Your Flutter App

Include Firebase in your Flutter project by adding the necessary dependencies in your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  firebase_core: latest_version
  firebase_auth: latest_version
  google_sign_in: latest_version

Run flutter pub get to install these packages.

Step 3: Implementing Google Sign-In

Start by initializing Firebase in your main.dart file:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

Create a new Dart file for handling the authentication process, e.g., auth.dart, and add the following code:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';

class AuthService {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  final GoogleSignIn googleSignIn = GoogleSignIn();

  Future signInWithGoogle() async {
    final GoogleSignInAccount? googleSignInAccount = await googleSignIn.signIn();
    if (googleSignInAccount != null) {
      final GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication;
      final AuthCredential credential = GoogleAuthProvider.credential(
        accessToken: googleSignInAuthentication.accessToken,
        idToken: googleSignInAuthentication.idToken,
      );
      final UserCredential authResult = await _auth.signInWithCredential(credential);
      final User? user = authResult.user;
      return user;
    }
    return null;
  }

  void signOutGoogle() async {
    await googleSignIn.signOut();
    print("User Signed Out");
  }
}

Finally, implement the UI and logic in your app to trigger Google sign-in and sign-out. For instance:

ElevatedButton(
  onPressed: () async {
    final user = await AuthService().signInWithGoogle();
    if (user != null) {
      print('Signed in as ${user.displayName}');
    }
  },
  child: Text('Sign in with Google'),
);

Conclusion

Google Authentication enhances the security and ease of use of your app. By following these steps, you can successfully integrate Google sign-in into your Flutter app using Firebase. For any additional configurations, especially for iOS, refer to the Firebase documentation for detailed guidance.

0 Comments:

Leave a Reply

Your email address will not be published. Required fields are marked *

Author *

Comment *