Implementing Social Login with OAuth2 (Google)

Summary
Summary
Summary
Summary

Learn to implement Google OAuth2 social login in Flutter using the google_sign_in plugin. This tutorial covers Google Cloud credential setup, plugin integration, Dart authentication code, and secure token handling, ensuring a smooth and secure sign-in flow for your mobile app.

Learn to implement Google OAuth2 social login in Flutter using the google_sign_in plugin. This tutorial covers Google Cloud credential setup, plugin integration, Dart authentication code, and secure token handling, ensuring a smooth and secure sign-in flow for your mobile app.

Learn to implement Google OAuth2 social login in Flutter using the google_sign_in plugin. This tutorial covers Google Cloud credential setup, plugin integration, Dart authentication code, and secure token handling, ensuring a smooth and secure sign-in flow for your mobile app.

Learn to implement Google OAuth2 social login in Flutter using the google_sign_in plugin. This tutorial covers Google Cloud credential setup, plugin integration, Dart authentication code, and secure token handling, ensuring a smooth and secure sign-in flow for your mobile app.

Key insights:
Key insights:
Key insights:
Key insights:
  • Configuring Google OAuth2 Credentials: Set up OAuth client in Google Cloud, add SHA-1, download config files.

  • Adding the google_sign_in Package: Install and import the plugin to enable OAuth2 support in Flutter.

  • Implementing the Authentication Flow: Use GoogleSignIn instance to prompt user, handle sign-in, and retrieve tokens.

  • Securing Tokens and Refresh Handling: Store tokens in secure storage and leverage signInSilently() for seamless sessions.

Introduction

Integrating social login in Flutter apps enhances user onboarding by reducing friction and leveraging proven authentication systems. Google’s OAuth2 mechanism, paired with Flutter’s google_sign_in plugin, streamlines sign-in without custom backends or UI. This tutorial walks you through configuring Google OAuth2 credentials, adding the plugin to your project, implementing the authentication flow, and securing tokens.

Configuring Google OAuth2 Credentials

  1. Open Google Cloud Console and create or select a project.

  2. Under API & Services, enable the “Google+ API” or “Google People API.”

  3. Navigate to Credentials and click “Create Credentials > OAuth client ID.”

  4. Choose “Android” (or “iOS”) application type:

    • For Android, supply your app’s package name and SHA-1 fingerprint.

    • For iOS, enter your bundle identifier.

  5. Download the generated google-services.json (Android) or GoogleService-Info.plist (iOS) and place it in your project’s android/app/ or ios/Runner/ directory.

  6. In android/app/build.gradle, add: • apply plugin: 'com.google.gms.google-services' • Under dependencies: classpath 'com.google.gms:google-services:4.3.8'.

Adding the google_sign_in Package

Open pubspec.yaml and add:

dependencies:
  flutter:
    sdk: flutter
  google_sign_in

Then run flutter pub get. Import the package where you’ll invoke sign-in:

import 'package:google_sign_in/google_sign_in.dart';

Implementing the Authentication Flow

Use GoogleSignIn to prompt the user and fetch tokens:

final GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: ['email', 'profile'],
);

Future<void> signInWithGoogle() async {
  try {
    final account = await _googleSignIn.signIn();
    if (account == null) return; // User canceled
    final auth = await account.authentication;
    // auth.accessToken & auth.idToken available here
    print('Access Token: ${auth.accessToken}');
  } catch (error) {
    print('Google sign-in failed: $error');
  }
}

Attach signInWithGoogle() to a button’s onPressed or app initialization logic. Always handle the null case when users cancel the prompt.

Securing Tokens and Refresh Handling

GoogleSignIn automatically refreshes tokens while the session is active. For persistent authentication across launches:

  1. Store auth.idToken or auth.accessToken in secure storage (e.g., flutter_secure_storage).

  2. On app start, call GoogleSignIn.signInSilently() to restore the session without user interaction.

  3. In case of token expiration, prompt a fresh sign-in or handle refresh errors gracefully.

final storage = FlutterSecureStorage();

Future<void> silentSignIn() async {
  final account = await _googleSignIn.signInSilently();
  if (account != null) {
    final auth = await account.authentication;
    await storage.write(key: 'accessToken', value: auth.accessToken);
  }
}

Vibe Studio

Vibe Studio, powered by Steve’s advanced AI agents, is a revolutionary no-code, conversational platform that empowers users to quickly and efficiently create full-stack Flutter applications integrated seamlessly with Firebase backend services. Ideal for solo founders, startups, and agile engineering teams, Vibe Studio allows users to visually manage and deploy Flutter apps, greatly accelerating the development process. The intuitive conversational interface simplifies complex development tasks, making app creation accessible even for non-coders.

Conclusion

By following these steps, you can quickly add Google OAuth2-based social login to your Flutter app. Proper credential configuration, use of the google_sign_in plugin, and secure token storage result in a smooth, secure user experience.

Introduction

Integrating social login in Flutter apps enhances user onboarding by reducing friction and leveraging proven authentication systems. Google’s OAuth2 mechanism, paired with Flutter’s google_sign_in plugin, streamlines sign-in without custom backends or UI. This tutorial walks you through configuring Google OAuth2 credentials, adding the plugin to your project, implementing the authentication flow, and securing tokens.

Configuring Google OAuth2 Credentials

  1. Open Google Cloud Console and create or select a project.

  2. Under API & Services, enable the “Google+ API” or “Google People API.”

  3. Navigate to Credentials and click “Create Credentials > OAuth client ID.”

  4. Choose “Android” (or “iOS”) application type:

    • For Android, supply your app’s package name and SHA-1 fingerprint.

    • For iOS, enter your bundle identifier.

  5. Download the generated google-services.json (Android) or GoogleService-Info.plist (iOS) and place it in your project’s android/app/ or ios/Runner/ directory.

  6. In android/app/build.gradle, add: • apply plugin: 'com.google.gms.google-services' • Under dependencies: classpath 'com.google.gms:google-services:4.3.8'.

Adding the google_sign_in Package

Open pubspec.yaml and add:

dependencies:
  flutter:
    sdk: flutter
  google_sign_in

Then run flutter pub get. Import the package where you’ll invoke sign-in:

import 'package:google_sign_in/google_sign_in.dart';

Implementing the Authentication Flow

Use GoogleSignIn to prompt the user and fetch tokens:

final GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: ['email', 'profile'],
);

Future<void> signInWithGoogle() async {
  try {
    final account = await _googleSignIn.signIn();
    if (account == null) return; // User canceled
    final auth = await account.authentication;
    // auth.accessToken & auth.idToken available here
    print('Access Token: ${auth.accessToken}');
  } catch (error) {
    print('Google sign-in failed: $error');
  }
}

Attach signInWithGoogle() to a button’s onPressed or app initialization logic. Always handle the null case when users cancel the prompt.

Securing Tokens and Refresh Handling

GoogleSignIn automatically refreshes tokens while the session is active. For persistent authentication across launches:

  1. Store auth.idToken or auth.accessToken in secure storage (e.g., flutter_secure_storage).

  2. On app start, call GoogleSignIn.signInSilently() to restore the session without user interaction.

  3. In case of token expiration, prompt a fresh sign-in or handle refresh errors gracefully.

final storage = FlutterSecureStorage();

Future<void> silentSignIn() async {
  final account = await _googleSignIn.signInSilently();
  if (account != null) {
    final auth = await account.authentication;
    await storage.write(key: 'accessToken', value: auth.accessToken);
  }
}

Vibe Studio

Vibe Studio, powered by Steve’s advanced AI agents, is a revolutionary no-code, conversational platform that empowers users to quickly and efficiently create full-stack Flutter applications integrated seamlessly with Firebase backend services. Ideal for solo founders, startups, and agile engineering teams, Vibe Studio allows users to visually manage and deploy Flutter apps, greatly accelerating the development process. The intuitive conversational interface simplifies complex development tasks, making app creation accessible even for non-coders.

Conclusion

By following these steps, you can quickly add Google OAuth2-based social login to your Flutter app. Proper credential configuration, use of the google_sign_in plugin, and secure token storage result in a smooth, secure user experience.

Build Flutter Apps Faster with Vibe Studio

Build Flutter Apps Faster with Vibe Studio

Build Flutter Apps Faster with Vibe Studio

Build Flutter Apps Faster with Vibe Studio

Vibe Studio is your AI-powered Flutter development companion. Skip boilerplate, build in real-time, and deploy without hassle. Start creating apps at lightning speed with zero setup.

Vibe Studio is your AI-powered Flutter development companion. Skip boilerplate, build in real-time, and deploy without hassle. Start creating apps at lightning speed with zero setup.

Vibe Studio is your AI-powered Flutter development companion. Skip boilerplate, build in real-time, and deploy without hassle. Start creating apps at lightning speed with zero setup.

Vibe Studio is your AI-powered Flutter development companion. Skip boilerplate, build in real-time, and deploy without hassle. Start creating apps at lightning speed with zero setup.

Other Insights

Other Insights

Other Insights

Other Insights

Join a growing community of builders today

Join a growing
community

of builders today

Join a growing

community

of builders today

© Steve • All Rights Reserved 2025

© Steve • All Rights Reserved 2025

© Steve • All Rights Reserved 2025

© Steve • All Rights Reserved 2025