このガイドでは、@codetrix-studio/capacitor-google-auth から @capgo/capacitor-social-login に移行するための包括的な手順を説明し、スムーズな移行と改善された認証エクスペリエンスを保証します。新しいプラグインは、複数のソーシャル認証プロバイダーを単一の一貫した API の下に統合します。
インストール
Section titled “インストール”-
古いパッケージを削除します。
Terminal window npm uninstall @codetrix-studio/capacitor-google-auth -
新しいパッケージをインストールします。
Terminal window npm install @capgo/capacitor-social-loginnpx cap sync
Google 認証設定の重要な変更点
Section titled “Google 認証設定の重要な変更点”Web クライアント ID の要件
Section titled “Web クライアント ID の要件”重要な変更: 更新されたプラグインでは、すべてのプラットフォームで Web クライアント ID を使用する必要があります。
次のことを行う必要があります。
- Web クライアント ID がない場合は、Google Cloud Console で Web クライアント ID を作成します (認証情報の取得方法)
- すべてのプラットフォームの
webClientIdフィールドでこの Web クライアント ID を使用します - Android の場合、SHA1 を使用して Android クライアント ID を作成する必要がありますが、これは検証目的のみであり、トークンは使用されません (Android セットアップ ガイド)
コードの変更
Section titled “コードの変更”変更のインポート
Section titled “変更のインポート”import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';import { SocialLogin } from '@capgo/capacitor-social-login';セットアップは、単純な GoogleAuth.initialize() 呼び出しから、ネストされた Google 構成を備えたより構造化された SocialLogin.initialize() に変換されます。
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 }});明示的なプロバイダ指定により、ログイン方法が GoogleAuth.signIn() から SocialLogin.login() に変更されます。
const user = await GoogleAuth.signIn();const res = await SocialLogin.login({ provider: 'google', options: { scopes: ['email', 'profile'], forceRefreshToken: true // if you need refresh token }});プラットフォーム固有の変更
Section titled “プラットフォーム固有の変更”Android
Section titled “Android”MainActivity.java(完全な Android セットアップ ガイド) を更新します。
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() {}}-
AppDelegate.swift に大きな変更は必要ありません (iOS セットアップ ガイド)
-
capacitor.config.jsonで設定を更新します。新しいプラグインではそれを使用しません。
{ "plugins": { "GoogleAuth": { "scopes": ["profile", "email"], "serverClientId": "xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com", "forceCodeForRefreshToken": true }}- Google サインイン メタ タグを使用している場合は、
index.htmlからこれらを削除します。
<meta name="google-signin-client_id" content="{your client id here}" /><meta name="google-signin-scope" content="profile email" />応答タイプの変更
Section titled “応答タイプの変更”認証応答は、プロバイダー情報、アクセス トークン、ID トークン、ユーザー プロファイル データを含む構造化オブジェクトを提供するようになりました。
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; }; };}応答構造には次のものが含まれます。
- プロバイダー: 認証プロバイダー (
'google') を識別します。 - result.accessToken: 有効期限のあるアクセス トークンの詳細
- result.idToken: 認証用のIDトークン
- result.profile: 電子メール、名前、画像 URL などのユーザー プロフィール情報
新しいパッケージは、Google を超える複数のソーシャル認証プロバイダーをサポートします。
この統一されたアプローチにより、以下が提供されます。
- すべてのプロバイダーにわたって一貫した API
- TypeScript サポートの改善
- エラー処理の改善
- 積極的なメンテナンスとコミュニティサポート
これらの追加プロバイダーの詳細なセットアップ手順については、メイン ドキュメント を確認してください。