跳过内容

开始使用

GitHub

您可以使用我们的 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.

如果您更喜欢手动设置,请运行以下命令并遵循以下平台特定的说明:

  1. 安装包

    终端窗口
    npm install @capgo/capacitor-social-login
  2. 同步本地项目

    终端窗口
    npx cap sync
  3. 在应用启动时初始化

    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' });

授权码和刷新

授权码和刷新
// For providers that support this mode
const 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.mode: 'offline' __CAPGO_KEEP_0__ serverAuthCode 从登录中返回。 在此模式下,注销、isLoggedIn、getAuthorizationCode 和 refresh 不可用。

仅作为您的后端令牌交换的输入使用。如果您需要在应用中调用,请 serverAuthCode 使用 SocialLogin.refresh() Apple google.mode: 'online' Apple

Android 简化设置 useProperTokenExchange: true 仅适用于 Android useBroadcastChannel: true 仅适用于 iOS

OAuth2 web redirect 流程

OAuth2 web redirect 流程

使用 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
  • Apple Sign-In on Android uses OAuth without external SDK dependencies.
  • Twitter/X uses standard OAuth 2.0 without external SDK dependencies.

仅包含Google Sign-In和Apple Sign-In:

plugins: {
SocialLogin: {
providers: {
google: true,
facebook: false,
apple: true,
twitter: false,
},
},
}
相关文档

Ionic 企业插件迁移解决方案

继续从 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 中的实现细节,和 双因素认证 双因素认证的实现细节。