Getting Started
复制一个包含安装步骤和本插件的完整 Markdown 指南的配置提示。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-social-login`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/social-login/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
安装您可以使用我们的 AI 助手设置来安装插件。使用以下命令将 Capgo 技能添加到您的 AI 工具中:
npx skills add https://github.com/cap-go/capacitor-skills --skill capacitor-plugins然后使用以下提示:
Use the `capacitor-plugins` skill from `cap-go/capacitor-skills` to install the `@capgo/capacitor-social-login` plugin in my project.如果您prefer 手动设置,安装插件并运行以下命令并遵循以下平台特定的说明:
-
安装包
终端窗口 npm install @capgo/capacitor-social-login -
同步本地项目
终端窗口 npx cap sync -
在应用启动时初始化
import { SocialLogin } from '@capgo/capacitor-social-login';await SocialLogin.initialize({google: {webClientId: 'your-google-web-client-id',iOSClientId: 'your-google-ios-client-id',iOSServerClientId: 'your-google-web-client-id',mode: 'online',},apple: {clientId: 'your-apple-service-id',useProperTokenExchange: true,useBroadcastChannel: true,},facebook: {appId: 'your-facebook-app-id',},twitter: {clientId: 'your-twitter-client-id',redirectUrl: 'myapp://oauth/twitter',},oauth2: {github: {appId: 'your-github-client-id',authorizationBaseUrl: 'https://github.com/login/oauth/authorize',accessTokenEndpoint: 'https://github.com/login/oauth/access_token',redirectUrl: 'myapp://oauth/github',scope: 'read:user user:email',pkceEnabled: true,},},});
核心流程示例
标题:核心流程示例登录
标题:登录await SocialLogin.login({ provider: 'google', options: { scopes: ['profile', 'email'] },});
await SocialLogin.login({ provider: 'oauth2', options: { providerId: 'github', scope: 'read:user user:email', },});会话检查
标题:会话检查const status = await SocialLogin.isLoggedIn({ provider: 'google' });await SocialLogin.logout({ provider: 'google' });Auth codes and refresh
Auth codes and refresh// For providers that support this modeconst authCodeResult = await SocialLogin.getAuthorizationCode({ provider: 'google' });await SocialLogin.refresh({ provider: 'google', options: {} as never });高级辅助工具
高级辅助工具const jwt = await SocialLogin.decodeIdToken({ idToken: 'eyJhbGciOi...',});
const { date } = await SocialLogin.getAccessTokenExpirationDate({ accessTokenExpirationDate: Date.now() + 3600 * 1000,});
const expired = await SocialLogin.isAccessTokenExpired({ accessTokenExpirationDate: Date.now() + 1000,});
const active = await SocialLogin.isRefreshTokenAvailable({ refreshToken: 'a-token' });供应商特定说明
供应商特定说明Google 离线模式
Google 离线模式google.mode: 'offline' 返回 serverAuthCode 从登录中返回。 在此模式下,注销、 isLoggedIn、 getAuthorizationCode 和 refresh 不可用。
仅作为您的后端令牌交换的输入使用。如果您需要在应用中调用, serverAuthCode 使用 SocialLogin.refresh() 而不是。 google.mode: 'online' Apple
Apple
严格令牌处理和Android简化设置OAuth2 web 重定向流 useProperTokenExchange: true OAuth2 web 重定向流 useBroadcastChannel: true 使用
仅作为您的后端令牌交换的输入使用。如果您需要在应用中调用,
使用而不是。 OAuth2LoginOptions.flow: 'redirect' 为那些导航离开页面的 Web 流程。
动态提供商依赖
标题:动态提供商依赖您可以配置要包含哪些提供商,以减少原生应用大小。这在您的应用只需要特定提供商时最有用。
添加提供商配置到 capacitor.config.ts:
import type { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = { appId: 'com.example.app', appName: 'MyApp', webDir: 'dist', plugins: { SocialLogin: { providers: { google: true, facebook: true, apple: true, twitter: false, }, logLevel: 1, }, },};
export default config;提供商值的含义:
true:表示提供商已启用,其原生依赖项已打包。false:表示提供商已禁用,其原生依赖项未打包。
重要细节:
- 运行
npx cap sync在更改提供商配置后 - 如果没有提供供应商配置,所有供应商都将默认为
true为了向后兼容性. - 禁用一个供应商时,
false使其在运行时不可用,即使该供应商只使用系统API. - 此配置仅影响iOS和Android;它不会影响Web.
- 在Android上使用Apple Sign-In时,使用OAuth而无需外部SDK依赖项.
- Twitter/X使用标准OAuth 2.0而无需外部SDK依赖项.
仅包含Google Sign-In和Apple Sign-In:
plugins: { SocialLogin: { providers: { google: true, facebook: false, apple: true, twitter: false, }, },}相关文档
相关文档部分“相关文档”- 整合概览
- 更好的认证集成
- OAuth2 和 OIDC 提供者
- 故障排除
- 隐私清单和 iOS URL 处理器
- 从 Ionic Auth Connect 迁移
- 社交登录 Auth Connect 迁移指南
- 迁移遗留提供者
- Ionic 企业插件迁移解决方案
继续从 Getting Started
继续从 Getting Started如果您正在使用 Getting Started 为了计划身份验证和帐户流程,连接它与 使用 @capgo/capacitor-social-login 对于本地能力的 @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 的实现细节,以及 双因素身份验证 对于双因素身份验证的实现细节