Google Auth 迁移到 @capgo/social-login
复制一个包含安装步骤和本插件的完整 Markdown 指南的配置提示。
本指南提供了从 @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.
-
删除旧包:
终端窗口 npm uninstall @codetrix-studio/capacitor-google-auth -
安装新包:
终端窗口 npm install @capgo/capacitor-social-loginnpx cap sync
Google Auth 配置中的重要变化
重要变化:Google Auth 配置Web 客户端 ID 的要求
Web 客户端 ID 的要求关键变化: 必须在所有平台上使用 Web 客户端 ID 的更新插件。
您需要:
- 在 Google Cloud Console 中创建一个 Web 客户端 ID,如果您没有一个(获取凭证的方法)
- 在此处使用 Web 客户端 ID
webClientId所有平台的字段 - 对于 Android,仍然需要使用您的 SHA1 创建一个 Android Client ID,但这仅用于验证目的 - 令牌将不会被使用(Android 设置指南)
Code Changes
标题:Code Changes导入更改
标题:导入更改import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';import { SocialLogin } from '@capgo/capacitor-social-login';初始化
标题:初始化设置从简单的 GoogleAuth.initialize() 调用转变为更结构化的 SocialLogin.initialize() 与嵌套的Google配置:
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 }});平台特定更改
标题:平台特定更改Android
标题: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() {}}iOS
iOS标题-
AppDelegate.swift中没有重大变化(iOS设置指南)
-
更新您的配置在
capacitor.config.json我们不再使用它在新插件:
{ "plugins": { "GoogleAuth": { "scopes": ["profile", "email"], "serverClientId": "xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com", "forceCodeForRefreshToken": true }}Web
Web标题- 移除 Google Sign-In 元标签:
index.html如果您曾经使用它们:
<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.profile:包含电子邮件、姓名和图片URL的用户资料
Additional Capabilities
额外功能The new package supports multiple social authentication providers beyond Google:
This unified approach provides:
- Consistent API across all providers
- Consistent __CAPGO_KEEP_0__ across all providers
- 更好的错误处理
- 积极的维护和社区支持
查看 主文档 了解这些额外提供商的详细设置指南。
Keep going from Google Auth Migration to @capgo/social-login
Section titled “Keep going from Google Auth Migration to @capgo/social-login”如果您正在使用 Google Auth Migration to @capgo/social-login 来规划身份验证和帐户流程,连接它 Using @capgo/capacitor-social-login for the native capability in Using @capgo/capacitor-social-login, @capgo/capacitor-social-login 在 @capgo/capacitor-social-login 的实现细节中 @capgo/capacitor-passkey 在 @capgo/capacitor-passkey 的实现细节中 @capgo/capacitor-native-biometric 在 @capgo/capacitor-native-biometric 的实现细节中,以及 双因素认证 在双因素认证的实现细节中