Zum Inhalt springen

Migrationsleitfaden von @capacitor-community/facebook-login zu @capgo/capacitor-social-login

Dieser Leitfaden bietet umfassende Anweisungen für die Migration von @capacitor-community/facebook-login zu @capgo/capacitor-social-login. Das neue Plugin modernisiert die Facebook-Authentifizierung mit einer einheitlichen API, die mehrere soziale Anbieter unterstützt, verbesserte TypeScript-Unterstützung und erweiterte Funktionen bietet.

  1. Entfernen Sie das alte Paket:

    Terminal-Fenster
    npm uninstall @capacitor-community/facebook-login
  2. Installieren Sie das neue Paket:

    Terminal-Fenster
    npm install @capgo/capacitor-social-login
    npx cap sync
import { FacebookLogin } from '@capacitor-community/facebook-login';
import { SocialLogin } from '@capgo/capacitor-social-login';

Wichtige Änderung: Das neue Paket erfordert eine explizite Einrichtung in Ihrem Code:

// Altes Paket erforderte keine explizite Initialisierung im Code
// Konfiguration erfolgte nur auf nativen Plattformen
// Neues Paket erfordert explizite Initialisierung
await SocialLogin.initialize({
facebook: {
appId: 'YOUR_FACEBOOK_APP_ID', // Erforderlich für Web und Android
clientToken: 'YOUR_CLIENT_TOKEN' // Erforderlich für Android
}
});

Die Login-Methode akzeptiert jetzt einen 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'
}
});

Die Antwortstruktur wurde mit einem umfassenderen Profilobjekt modernisiert:

// Alter Antworttyp
interface FacebookLoginResponse {
accessToken: {
applicationId: string;
userId: string;
token: string;
expires: string;
};
recentlyGrantedPermissions: string[];
recentlyDeniedPermissions: string[];
}
// Neuer Antworttyp
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;
};
};
}

Hauptunterschiede:

  • Die Antwort enthält jetzt ein provider-Feld zur Identifizierung des Authentifizierungsanbieters
  • Detaillierteres profile-Objekt mit zusätzlichen Benutzerinformationen
  • Konsistente Struktur über alle sozialen Login-Anbieter hinweg
const result = await FacebookLogin.getCurrentAccessToken();
const isLoggedIn = result && result.accessToken;
const status = await SocialLogin.isLoggedIn({
provider: 'facebook'
});
const isLoggedIn = status.isLoggedIn;
await FacebookLogin.logout();
await SocialLogin.logout({
provider: 'facebook'
});

Die Konfiguration erfolgt jetzt über die Initialize-Methode:

// AndroidManifest.xml-Änderungen bleiben gleich
// strings.xml wird irrelevant
// Zusätzlich in Ihrem Code initialisieren:
await SocialLogin.initialize({
facebook: {
appId: 'your-app-id',
clientToken: 'your-client-token' // Neue Anforderung
}
});

Wichtig: Client-Token ist jetzt für die Android-Authentifizierung erforderlich.

  1. Die iOS-Einrichtung in AppDelegate.swift bleibt gleich:
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. Die Info.plist-Konfiguration bleibt gleich:
<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>

Zusammenfassung der Breaking Changes bei der Migration:

  1. Explizite Initialisierung ist jetzt erforderlich - Muss initialize() vor der Verwendung aufrufen
  2. Struktur des Antwortobjekts hat sich erheblich geändert - Neues verschachteltes Ergebnisformat mit erweiterten Profildaten
  3. Client-Token ist jetzt für Android erforderlich - Zusätzliche Konfiguration erforderlich
  4. Unterschiedliche Methodennamen und Parameterstrukturen - Provider-basierter Ansatz
  5. Fehlerbehandlung und Fehlertypen haben sich geändert - Detailliertere Fehlerinformationen

Das neue Plugin bietet:

  • Einheitliche API über mehrere soziale Anbieter hinweg (Google, Apple, Facebook)
  • Verbesserte TypeScript-Unterstützung mit besseren Typdefinitionen
  • Erweiterte Profildaten mit mehr Benutzerinformationen
  • Aktive Wartung und Community-Support
  • Konsistente Fehlerbehandlung über alle Anbieter hinweg
  • Bessere Token-Verwaltung mit ordnungsgemäßer Ablaufbehandlung

Für detailliertere Einrichtungsanweisungen konsultieren Sie bitte die offizielle Dokumentation.