Passer au contenu

Migration Guide from @capacitor-community/facebook-login to @capgo/capacitor-social-login

Ce contenu n'est pas encore disponible dans votre langue.

Overview

This guide provides comprehensive instructions for migrating from @capacitor-community/facebook-login to @capgo/capacitor-social-login. The new plugin modernizes Facebook authentication with a unified API that supports multiple social providers, improved TypeScript support, and enhanced capabilities.

Installation

  1. Remove the old package:

    Terminal window
    npm uninstall @capacitor-community/facebook-login
  2. Install the new package:

    Terminal window
    npm install @capgo/capacitor-social-login
    npx cap sync

Code Changes

Import Changes

import { FacebookLogin } from '@capacitor-community/facebook-login';
import { SocialLogin } from '@capgo/capacitor-social-login';

Initialization

Key Change: The new package requires explicit setup in your code:

// Old package required no explicit initialization in code
// Configuration was done only in native platforms
// New package requires explicit initialization
await SocialLogin.initialize({
facebook: {
appId: 'YOUR_FACEBOOK_APP_ID', // Required for web and Android
clientToken: 'YOUR_CLIENT_TOKEN' // Required for Android
}
});

Login

The login method now accepts a provider parameter:

const FACEBOOK_PERMISSIONS = ['email', 'public_profile'];
const result = await FacebookLogin.login({ permissions: FACEBOOK_PERMISSIONS });
const result = await SocialLogin.login({
provider: 'facebook',
options: {
permissions: ['email', 'public_profile'],
limitedLogin: false,
nonce: 'optional_nonce'
}
});

Response Type Changes

The response structure has been modernized with a more comprehensive profile object:

// Old response type
interface FacebookLoginResponse {
accessToken: {
applicationId: string;
userId: string;
token: string;
expires: string;
};
recentlyGrantedPermissions: string[];
recentlyDeniedPermissions: string[];
}
// New response type
interface FacebookLoginResponse {
provider: 'facebook';
result: {
accessToken: {
token: string;
applicationId?: string;
expires?: string;
userId?: string;
permissions?: string[];
declinedPermissions?: string[];
} | null;
idToken: string | null;
profile: {
userID: string;
email: string | null;
friendIDs: string[];
birthday: string | null;
ageRange: { min?: number; max?: number } | null;
gender: string | null;
location: { id: string; name: string } | null;
hometown: { id: string; name: string } | null;
profileURL: string | null;
name: string | null;
imageURL: string | null;
};
};
}

Key Differences:

  • Response now includes a provider field identifying the authentication provider
  • More detailed profile object with additional user information
  • Consistent structure across all social login providers

Checking Login Status

const result = await FacebookLogin.getCurrentAccessToken();
const isLoggedIn = result && result.accessToken;
const status = await SocialLogin.isLoggedIn({
provider: 'facebook'
});
const isLoggedIn = status.isLoggedIn;

Logout

await FacebookLogin.logout();
await SocialLogin.logout({
provider: 'facebook'
});

Platform Specific Changes

Android Setup

Configuration is now handled through the initialize method:

// AndroidManifest.xml changes remain the same
// strings.xml become irrelevant
// Additionally initialize in your code:
await SocialLogin.initialize({
facebook: {
appId: 'your-app-id',
clientToken: 'your-client-token' // New requirement
}
});

Important: Client token is now required for Android authentication.

iOS Setup

  1. The iOS setup in AppDelegate.swift remains the same:
import FBSDKCoreKit
// In application:didFinishLaunchingWithOptions:
FBSDKCoreKit.ApplicationDelegate.shared.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
// In application:openURL:options:
ApplicationDelegate.shared.application(
app,
open: url,
sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplication.OpenURLOptionsKey.annotation]
)
  1. The Info.plist configuration remains the same:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb[APP_ID]</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>[APP_ID]</string>
<key>FacebookClientToken</key>
<string>[CLIENT_TOKEN]</string>
<key>FacebookDisplayName</key>
<string>[APP_NAME]</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbauth</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>

Breaking Changes

Summary of breaking changes when migrating:

  1. Explicit initialization is now required - Must call initialize() before use
  2. Response object structure has changed significantly - New nested result format with enhanced profile data
  3. Client token is now required for Android - Additional configuration needed
  4. Different method names and parameter structures - Provider-based approach
  5. Error handling and error types have changed - More detailed error information

Key Advantages

The new plugin provides:

  • Unified API across multiple social providers (Google, Apple, Facebook)
  • Improved TypeScript support with better type definitions
  • Enhanced profile data with more user information
  • Active maintenance and community support
  • Consistent error handling across all providers
  • Better token management with proper expiration handling

For more detailed setup instructions, please refer to the official documentation.