コンテンツにジャンプ

Google Auth @capgo/social-login への移行

概要

概要

このガイドでは、 @codetrix-studio/capacitor-google-auth から @capgo/capacitor-social-login, ensuring a smooth transition and improved authentication experience. The new plugin unifies multiple social authentication providers under a single, consistent API.

  1. セクションのタイトル “インストール”

    古いパッケージを削除する:
    npm uninstall @codetrix-studio/capacitor-google-auth
  2. コピー

    新しいパッケージをインストールする:
    npm install @capgo/capacitor-social-login
    npx cap sync

Google Auth 設定における重要な変更

重要な変更: Google Auth 設定

Web クライアント ID の要件

重要な変更: Web クライアント ID

重要な変更: 最新のプラグインでは、すべてのプラットフォームで Web クライアント ID を使用する必要があります。

必要な手順は次のとおりです。

  1. Google Cloud Console で Web クライアント ID を作成する (クレデンシャルを取得する方法)
  2. すべてのプラットフォームでこの Web クライアント ID を使用する webClientId Android の場合、SHA1 と共に Android クライアント ID を作成する必要がありますが、このトークンは使用されません (
  3. For Android, you still need to create an Android Client ID with your SHA1, but this is only for verification purposes - the token won’t be used (Android セットアップ ガイド)
import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';
import { SocialLogin } from '@capgo/capacitor-social-login';

セットアップは、単純な呼び出しから構造化された呼び出しに変わり、Google の構成がネストされます: GoogleAuth.initialize() コピー 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
}
});

プラットフォーム固有の変更

プラットフォーム固有の変更セクション
  1. 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() {}
}
  1. AppDelegate.swift (に大きな変更は必要ありません(iOSのセットアップガイド)

  2. 、新しいプラグインでは使用していないため、 capacitor.config.jsonクリップボードにコピー

{
"plugins": {
"GoogleAuth": {
"scopes": ["profile", "email"],
"serverClientId": "xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
"forceCodeForRefreshToken": true
}
}
  1. 使用していましたら index.html if you were using them:
<meta name="google-signin-client_id" content="{your client id here}" />
<meta name="google-signin-scope" content="profile email" />

認証応答は、プロバイダ情報、承認トークン、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;
};
};
}

レスポンス構造には以下の要素が含まれます。

  • provider: 認証プロバイダを識別する'google')
  • result.accessToken: 有効期限が切れるまでの承認トークン詳細
  • result.idToken: 認証用のIDトークン
  • result.profil: ユーザープロフィール情報、メール、名前、画像URLを含みます

新しいパッケージは、Google 以外の複数のソーシャル認証プロバイダーをサポートします:

統一されたアプローチにより、以下の利点があります:

  • すべてのプロバイダーで API が一貫しています
  • 改善されたTypeScriptサポート
  • エラー処理の向上
  • アクティブなメンテナンスとコミュニティサポート

確認してください 主なドキュメント 詳細なセットアップ手順については、以下の追加プロバイダーに関するドキュメントを参照してください。