Zum Inhalt springen

Google Auth Migration zu @capgo/social-login

GitHub

Dieser Leitfaden bietet umfassende Schritte für die Migration von @codetrix-studio/capacitor-google-auth zu @capgo/capacitor-social-login, um einen reibungslosen Übergang und eine verbesserte Authentifizierungserfahrung zu gewährleisten. Das neue Plugin bündelt mehrere Social-Authentifizierungsanbieter unter einer einheitlichen API.

  1. Altes Paket entfernen:

    Terminalfenster
    npm uninstall @codetrix-studio/capacitor-google-auth
  2. Neues Paket installieren:

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

Kritische Änderung: Das aktualisierte Plugin erfordert die Verwendung einer Web-Client-ID auf allen Plattformen.

Sie müssen Folgendes tun:

  1. Erstellen Sie eine Web-Client-ID in der Google Cloud Console, falls Sie noch keine haben (So erhalten Sie die Zugangsdaten)
  2. Verwenden Sie diese Web-Client-ID im webClientId Feld für alle Plattformen
  3. Für Android müssen Sie weiterhin eine Android-Client-ID mit Ihrer SHA1 erstellen, dies dient jedoch nur der Verifizierung – das Token wird nicht verwendet (Android-Setup-Anleitung)
import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';
import { SocialLogin } from '@capgo/capacitor-social-login';

Die Einrichtung wandelt sich von einer einfachen GoogleAuth.initialize() Aufruf zu einer strukturierteren SocialLogin.initialize() mit verschachtelter Google-Konfiguration:

GoogleAuth.initialize({
clientId: 'CLIENT_ID.apps.googleusercontent.com',
scopes: ['profile', 'email'],
grantOfflineAccess: true,
});
await SocialLogin.initialize({
google: {
webClientId: 'WEB_CLIENT_ID.apps.googleusercontent.com', // Use Web Client ID for all platforms
iOSClientId: 'IOS_CLIENT_ID', // for iOS
mode: 'offline' // replaces grantOfflineAccess
}
});

Die Login-Methode ändert sich von GoogleAuth.signIn() zu SocialLogin.login() mit expliziter Provider-Angabe:

const user = await GoogleAuth.signIn();
const res = await SocialLogin.login({
provider: 'google',
options: {
scopes: ['email', 'profile'],
forceRefreshToken: true // if you need refresh token
}
});
  1. Aktualisiere dein MainActivity.java (Vollständiger Android-Setup-Guide):
import ee.forgr.capacitor.social.login.GoogleProvider;
import ee.forgr.capacitor.social.login.SocialLoginPlugin;
import ee.forgr.capacitor.social.login.ModifiedMainActivityForSocialLoginPlugin;
import com.getcapacitor.PluginHandle;
import com.getcapacitor.Plugin;
import android.content.Intent;
import android.util.Log;
public class MainActivity extends BridgeActivity {
public class MainActivity extends BridgeActivity implements ModifiedMainActivityForSocialLoginPlugin {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode >= GoogleProvider.REQUEST_AUTHORIZE_GOOGLE_MIN && requestCode < GoogleProvider.REQUEST_AUTHORIZE_GOOGLE_MAX) {
PluginHandle pluginHandle = getBridge().getPlugin("SocialLogin");
if (pluginHandle == null) {
Log.i("Google Activity Result", "SocialLogin login handle is null");
return;
}
Plugin plugin = pluginHandle.getInstance();
if (!(plugin instanceof SocialLoginPlugin)) {
Log.i("Google Activity Result", "SocialLogin plugin instance is not SocialLoginPlugin");
return;
}
((SocialLoginPlugin) plugin).handleGoogleLoginIntent(requestCode, data);
}
}
public void IHaveModifiedTheMainActivityForTheUseWithSocialLoginPlugin() {}
}
  1. Keine größeren Änderungen in AppDelegate.swift erforderlich (iOS-Setup-Guide)

  2. Aktualisiere deine Konfiguration in capacitor.config.json, wir verwenden es im neuen Plugin nicht:

{
"plugins": {
"GoogleAuth": {
"scopes": ["profile", "email"],
"serverClientId": "xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
"forceCodeForRefreshToken": true
}
}
  1. Entferne die Google Sign-In-Metatags aus deinem index.html falls du sie verwendet hast:
<meta name="google-signin-client_id" content="{your client id here}" />
<meta name="google-signin-scope" content="profile email" />

Die Authentifizierungsantwort liefert nun ein strukturiertes Objekt mit Anbieterinformationen, Access-Tokens, ID-Tokens und Benutzerprofil-Daten:

interface GoogleLoginResponse {
provider: 'google';
result: {
accessToken: {
token: string;
expires: string;
// ... other token fields
} | null;
idToken: string | null;
profile: {
email: string | null;
familyName: string | null;
givenName: string | null;
id: string | null;
name: string | null;
imageUrl: string | null;
};
};
}

Die Antwortstruktur umfasst:

  • Provider: Identifiziert den Authentifizierungsanbieter ('google')
  • result.accessToken: Details des Access Tokens mit Ablaufdatum
  • result.idToken: ID-Token für die Authentifizierung
  • result.profile: Benutzerprofilinformationen, einschließlich E-Mail, Name und Bild-URL

Das neue Paket unterstützt mehrere soziale Authentifizierungsanbieter neben Google:

Dieser einheitliche Ansatz bietet:

  • Konsistente API über alle Anbieter hinweg
  • Verbesserte TypeScript-Unterstützung
  • Bessere Fehlerbehandlung
  • Aktive Wartung und Community-Support

Schau in die Hauptdokumentation für detaillierte Einrichtungshinweise zu diesen zusätzlichen Anbietern.

Weiter geht es von der Google Auth Migration zu @capgo/social-login

Abschnitt mit dem Titel „Weiter von Google Auth Migration zu @capgo/social-login“

Wenn du es nutzt Google Auth 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.