Zum Inhalt springen

Facebook Login Migration zu @capgo/social-login

GitHub

Diese Anleitung bietet eine umfassende Anleitung für die Migration von @capacitor-community/facebook-login zu @capgo/capacitor-social-login. Das neue Plugin modernisiert die Facebook-Authentifizierung mit einem einheitlichen API, das mehrere Social Provider, verbesserten TypeScript-Support und erweiterte Funktionen bietet.

  1. Entferne das alte Paket:

    Terminalfenster
    npm uninstall @capacitor-community/facebook-login
  2. Installiere das neue Package:

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

Key Change: Das neue Package erfordert ein explizites Setup in deinem 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
}
});

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 durch ein umfassenderes Profil-Objekt modernisiert:

// 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;
};
};
}

Wichtigste Unterschiede:

  • Die Response enthält nun ein provider Feld zur Identifizierung des Authentication-Providers
  • Detaillierter profile Objekt mit zusätzlichen Benutzerinformationen
  • Einheitliche Struktur über alle Social-Login-Provider 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 nun über die initialize-Methode:

// 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
}
});

Wichtig: Für die Android-Authentifizierung ist nun ein Client-Token erforderlich.

  1. Das iOS-Setup in AppDelegate.swift bleibt unverändert:
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. Der Info.plist die Konfiguration bleibt unverändert:
<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 nun erforderlich - Must call initialize() vor der Verwendung
  2. Struktur des Response-Objekts hat sich stark geändert - Neues verschachteltes Result-Format mit erweiterten Profildaten
  3. Client-Token ist nun für Android erforderlich - Zusätzliche Konfiguration notwendig
  4. Geänderte Methodennamen und Parameterstrukturen - Provider-basierter Ansatz
  5. Fehlerbehandlung und Fehlertypen haben sich geändert - Detailliertere Fehlermeldungen

Das neue Plugin bietet:

  • Einheitliches API über mehrere Social-Provider hinweg (Google, Apple, Facebook)
  • Verbesserte TypeScript-Unterstützung mit besseren Type-Definitionen
  • Erweiterte Profildaten mit mehr Benutzerinformationen
  • Aktive Wartung und Community-Support
  • Einheitliches Error-Handling über alle Provider hinweg
  • Besseres Token-Management mit korrektem Expiration-Handling

Detaillierte Setup-Anweisungen findest du in der offiziellen Dokumentation.

Wenn du es nutzt Facebook Login Migration zu @capgo/social-login Um Authentifizierungs- und Account-Flows zu planen, verbinde es mit @capgo/capacitor-social-login verwenden für die native Funktion bei der Verwendung von @capgo/capacitor-social-login, @capgo/capacitor-social-login für die Implementierungsdetails in @capgo/capacitor-social-login, @capgo/capacitor-passkey für die Implementierungsdetails in @capgo/capacitor-passkey, @capgo/capacitor-native-biometric für die Implementierungsdetails in @capgo/capacitor-native-biometric, und Zwei-Faktor-Authentifizierung für die Implementierungsdetails zur Zwei-Faktor-Authentifizierung.