Skip to content

Kotlin Codes

  • Home
  • Flutter
  • Kotlin
  • SwiftUI
  • About Me
  • Home
    • Blog
    • Privacy Policy
  • Flutter
    • Widgets In Flutter
      • Cupertino Widgets
      • iOS Styling Flutter
    • Database & Storage
    • State Management in Flutter
    • Performance Optimization
    • Networking & APIs
    • Testing & Debugging
  • Kotlin
    • Kotlin XML Development(Traditional View-based UI)
      • Introduction to XML UI Development
      • State Management and Architecture
      • Advanced Topics
      • Firebase and Cloud Integration
      • UI Components and Customization
      • Media and Visual Enhancements
      • Navigation and Data Handling
      • UI Performance Optimization
      • Networking and Data Management
    • Jetpack Compose
      • UI Elements
      • Kotlin Multiplatform
      • Accessibility
      • Animation
      • Core Concepts
      • Custom Drawing
      • Interoperability
      • Layouts
      • State Management
      • Modifiers
      • Navigation
      • Testing
      • Theming
      • Performance
    • Kotin-CodeChallenge
  • SwiftUI
  • About Me

Handling Social Media Authentication Flows and Access Tokens in Flutter

April 23, 2025May 15, 2025 Sayandh

In modern app development, integrating social media authentication is crucial for providing users with seamless and familiar login experiences. This article delves into the intricacies of handling social media authentication flows and access tokens in Flutter. We’ll cover the setup, implementation, and best practices for integrating popular social media platforms, such as Google, Facebook, and Apple, ensuring a secure and user-friendly experience.

Understanding Social Media Authentication

Social media authentication allows users to log into your application using their existing social media accounts, streamlining the sign-up process and enhancing user convenience. This process relies on OAuth (Open Authorization), an open standard for token-based authentication and authorization.

Why Integrate Social Media Authentication?

  • User Convenience: Eliminates the need to create and remember new usernames and passwords.
  • Increased Conversion: Simplifies the sign-up process, leading to higher user adoption rates.
  • Data Access (With Permission): Allows access to user data (e.g., email, profile info) for personalized experiences (with user consent).

Key Concepts: OAuth and Access Tokens

  • OAuth (Open Authorization): A standard protocol that allows users to grant third-party applications limited access to their resources on an authorization server without sharing their credentials.
  • Access Token: A credential issued by the authorization server to the application after successful authentication. It allows the application to access specific user resources for a limited time.
  • Refresh Token: A long-lived token used to obtain new access tokens without requiring the user to re-authenticate.

Setting Up Flutter Project and Dependencies

Before diving into implementation, ensure your Flutter project is set up correctly and includes the necessary dependencies.

Step 1: Create a New Flutter Project

flutter create social_auth_app

Step 2: Add Dependencies

Add the following dependencies to your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^2.15.0 # For Firebase integration
  firebase_auth: ^4.6.3 # For Firebase Authentication
  google_sign_in: ^6.1.4 # For Google Sign-In
  flutter_facebook_auth: ^5.0.6 # For Facebook Authentication
  sign_in_with_apple: ^5.0.0 # For Apple Sign-In

Run flutter pub get to install the dependencies.

Integrating Google Sign-In

Google Sign-In allows users to authenticate with their Google accounts. Here’s how to integrate it into your Flutter app:

Step 1: Configure Firebase Project

  1. Create a new project in the Firebase Console.
  2. Enable Google Sign-In as an authentication provider.
  3. Download the google-services.json file and add it to your android/app directory.
  4. Configure Firebase for iOS by downloading GoogleService-Info.plist and adding it to your Xcode project.

Step 2: Initialize Firebase

Initialize Firebase in your main.dart file:

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Social Auth Demo',
      home: LoginPage(),
    );
  }
}

class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Login')),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            // Google Sign-In Logic
          },
          child: Text('Sign in with Google'),
        ),
      ),
    );
  }
}

Step 3: Implement Google Sign-In

Add the following Google Sign-In logic to your LoginPage:

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

class LoginPage extends StatelessWidget {
  Future _handleGoogleSignIn() async {
    try {
      final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
      final GoogleSignInAuthentication googleAuth = await googleUser!.authentication;

      final OAuthCredential credential = GoogleAuthProvider.credential(
        accessToken: googleAuth.accessToken,
        idToken: googleAuth.idToken,
      );

      await FirebaseAuth.instance.signInWithCredential(credential);
      print("Google Sign-In successful");
    } catch (e) {
      print("Error signing in with Google: $e");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Login')),
      body: Center(
        child: ElevatedButton(
          onPressed: _handleGoogleSignIn,
          child: Text('Sign in with Google'),
        ),
      ),
    );
  }
}

This code performs the following steps:

  1. Launches the Google Sign-In flow.
  2. Retrieves the Google authentication credentials (access token and ID token).
  3. Exchanges the Google credentials with Firebase to authenticate the user.
  4. Prints a success or error message to the console.

Integrating Facebook Authentication

Facebook Authentication allows users to log in using their Facebook accounts. Follow these steps to integrate it:

Step 1: Configure Facebook App

  1. Create a new app in the Facebook Developer Console.
  2. Configure the app by adding platform (Android, iOS) details.
  3. Get the App ID and App Secret for your Facebook app.
  4. Configure the Login Product and enable “Login with the SDK”.
  5. Add the required permissions (e.g., email, public_profile).

Step 2: Update Flutter App

Update your Flutter project with the necessary Facebook app configurations:

Android Configuration
  1. Add the Facebook App ID and Client Token to your android/app/src/main/res/values/strings.xml file:

    YOUR_FACEBOOK_APP_ID
    fbYOUR_FACEBOOK_APP_ID
    YOUR_FACEBOOK_CLIENT_TOKEN

  1. Update your android/app/build.gradle file:
android {
    defaultConfig {
        manifestPlaceholders = [
            facebook_app_id: "YOUR_FACEBOOK_APP_ID",
            facebook_client_token: "YOUR_FACEBOOK_CLIENT_TOKEN"
        ]
    }
}
iOS Configuration
  1. Update your ios/Runner/Info.plist file:
CFBundleURLTypes

    
        CFBundleURLSchemes
        
            fbYOUR_FACEBOOK_APP_ID
        
    

FacebookAppID
YOUR_FACEBOOK_APP_ID
FacebookClientToken
YOUR_FACEBOOK_CLIENT_TOKEN
LSApplicationQueriesSchemes

    fbapi
    fb-messenger-share-api

Step 3: Implement Facebook Authentication

Implement the Facebook Authentication logic in your LoginPage:

import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class LoginPage extends StatelessWidget {
  Future _handleFacebookSignIn() async {
    try {
      final LoginResult result = await FacebookAuth.instance.login();

      if (result.status == LoginStatus.success) {
        final OAuthCredential credential = FacebookAuthProvider.credential(result.accessToken!.token);
        await FirebaseAuth.instance.signInWithCredential(credential);
        print("Facebook Sign-In successful");
      } else {
        print("Facebook Sign-In failed: ${result.status}");
        print("Error: ${result.message}");
      }
    } catch (e) {
      print("Error signing in with Facebook: $e");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Login')),
      body: Center(
        child: ElevatedButton(
          onPressed: _handleFacebookSignIn,
          child: Text('Sign in with Facebook'),
        ),
      ),
    );
  }
}

This code performs the following steps:

  1. Launches the Facebook login flow using the flutter_facebook_auth package.
  2. Retrieves the access token after successful login.
  3. Exchanges the Facebook access token with Firebase to authenticate the user.
  4. Prints a success or error message to the console.

Integrating Sign In With Apple

Apple Sign In allows users to authenticate using their Apple ID. It’s essential for apps targeting iOS users.

Step 1: Configure Apple Sign-In

  1. Enable Sign In with Apple in your Firebase project.
  2. Register your app’s domain in the Apple Developer portal.
  3. Configure your app’s Associated Domains to include applinks:your-app-domain.com.
  4. Enable Sign In with Apple capability in Xcode.

Step 2: Implement Apple Sign-In

Add the following Apple Sign-In logic to your LoginPage:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
import 'package:flutter/material.dart';

class LoginPage extends StatelessWidget {
  Future _handleAppleSignIn() async {
    try {
      final AuthorizationCredentialAppleID appleCredential = await SignInWithApple.getAppleIDCredential(
        scopes: [
          AppleIDAuthorizationScopes.email,
          AppleIDAuthorizationScopes.fullName,
        ],
      );

      final OAuthCredential credential = OAuthProvider('apple.com').credential(
        idToken: appleCredential.identityToken,
        accessToken: appleCredential.authorizationCode,
      );

      await FirebaseAuth.instance.signInWithCredential(credential);
      print("Apple Sign-In successful");
    } catch (e) {
      print("Error signing in with Apple: $e");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Login')),
      body: Center(
        child: ElevatedButton(
          onPressed: _handleAppleSignIn,
          child: Text('Sign in with Apple'),
        ),
      ),
    );
  }
}

This code performs the following steps:

  1. Launches the Apple Sign-In flow.
  2. Retrieves the Apple ID credentials (identity token and authorization code).
  3. Exchanges the Apple credentials with Firebase to authenticate the user.
  4. Prints a success or error message to the console.

Handling Access Tokens Securely

Access tokens should be handled securely to protect user data. Follow these best practices:

  • Secure Storage: Store access tokens in secure storage provided by the operating system (e.g., Keychain on iOS, Keystore on Android).
  • Token Refresh: Implement token refresh mechanisms to obtain new access tokens when the current token expires, using refresh tokens.
  • Minimize Permissions: Only request the minimum permissions required for your application to function.
  • Token Revocation: Provide users with the ability to revoke access to their social media accounts from your application.

Revoking Social Media Access

Provide a way for users to disconnect their social media accounts from your app. This usually involves revoking the access token associated with the account.

Revoking Google Access

import 'package:google_sign_in/google_sign_in.dart';

Future _signOutGoogle() async {
  try {
    await GoogleSignIn().signOut();
    print("User signed out of Google");
  } catch (e) {
    print("Error signing out of Google: $e");
  }
}

Revoking Facebook Access

import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';

Future _logOutFacebook() async {
  try {
    await FacebookAuth.instance.logOut();
    print("User logged out of Facebook");
  } catch (e) {
    print("Error logging out of Facebook: $e");
  }
}

Revoking Apple Access

Apple does not provide a direct method to revoke access tokens from the app side. Users can manage connected apps via their Apple ID settings.

Error Handling and Edge Cases

Proper error handling is crucial for providing a smooth user experience. Handle the following common edge cases:

  • Network Issues: Handle cases where the user has no network connectivity.
  • Authentication Failures: Handle scenarios where the authentication process fails due to invalid credentials or permission issues.
  • Cancelled Authentication: Handle cases where the user cancels the authentication flow.

Conclusion

Integrating social media authentication in Flutter can significantly enhance user experience by providing seamless login options. By leveraging Firebase, Google Sign-In, Facebook Authentication, and Apple Sign In, you can offer a convenient and secure way for users to access your application. Remember to handle access tokens securely, provide options for users to revoke access, and implement robust error handling to create a polished and user-friendly application.

Beyond This Article: Your Next Discovery Awaits

Integrating with Firebase Authentication in Flutter
Using Packages for Social Login and Sharing in Flutter
Handling Social Media Authentication Flows in Flutter
Using Firebase Authentication for User Sign-In and Sign-Up in Flutter
Building Social Media Features in XML-Based Apps
Using Packages for Social Login (e.g., flutter_facebook_auth, google_sign_in) in Flutter
Tagged with Access Token Management, Android Firebase Authentication, Apple Sign-In Flutter, Facebook Authentication Flutter, Flutter Authentication Flows, Flutter Firebase Authentication, Flutter Social Authentication, Google Sign-In Flutter, OAuth in Flutter, Social Login Flutter
  • Advanced Concepts

Post navigation

Previous Post

Using Packages for Social Login (e.g., flutter_facebook_auth, google_sign_in) in Flutter

Next Post

Implementing Functionality to Share Content to Social Media Platforms in Flutter

Recents

  • Performing Integration Testing to Validate the Interaction Between Different Parts of Your Flutter Application
  • Using Packages Like upgrader to Facilitate the In-App Update Process in Flutter
  • Implementing In-App Updates to Allow Users to Update Your App Without Leaving It in Flutter
  • Analyzing Analytics Data to Gain Insights into User Engagement and Identify Areas for Improvement in Flutter
  • Integrating with Crashlytics for Automatic Reporting of Crashes and Errors in Flutter
  • Dart
  • Flutter
    • Advanced Concepts
    • Animations & UI Enhancements
    • Data Handling (JSON, REST APIs, Databases)
    • Database & Storage
    • Input Widgets
    • iOS Styling Flutter
    • Layout Widgets
    • Navigation and Routing
    • Networking & APIs
    • Performance Optimization
    • Platform Integration (Native Features)
    • State Management (Provider, BLoC, Riverpod)
    • State Management in Flutter
    • Testing (Unit, Widget, Integration)
    • Testing & Debugging
    • UI Basics
    • Widgets In Flutter
      • Cupertino Widgets
  • Kotlin
    • Jetpack Compose
      • Accessibility
      • Animation
      • Core Concepts
      • Custom Drawing
      • Interoperability
      • Kotlin Multiplatform
      • Layouts
      • Modifiers
      • Navigation
      • Performance
      • State Management
      • Testing
      • Theming
      • UI Elements
    • Kotin-CodeChallenge
    • Kotlin XML Development(Traditional View-based UI)
      • Accessibility
      • Advanced Topics
      • Advanced Topics & Custom Views
      • Animation
      • Data Binding
      • Drawables
      • Firebase and Cloud Integration
      • Introduction to XML UI Development
      • Kotlin Integration & Patterns
      • Layouts
      • Media and Visual Enhancements
      • Navigation and Data Handling
      • Networking and Data Management
      • RecyclerView
      • State Management and Architecture
      • Styles & Themes
      • UI Components and Customization
      • UI Performance Optimization
      • View Binding
      • Views
      • XML Techniques
  • SwiftUI

© KotlinCodes. Explore the latest Kotlin tutorials, Flutter guides, and Dart programming tips. | Learn Kotlin | Flutter Development | Dart Programming | Flutter Widgets