跳过内容

Apple Sign-In Migration 到 @capgo/social-login

GitHub

本指南概述了从遗留插件到现代 @capacitor-community/apple-sign-in 包的过渡。新插件提供了多个社交身份验证提供者的统一接口、改进的 TypeScript 支持和积极维护。 @capgo/capacitor-social-login 安装

标题为“安装”

context
  1. 移除旧包:

    终端窗口
    npm uninstall @capacitor-community/apple-sign-in
  2. 安装新包:

    终端窗口
    npm install @capgo/capacitor-social-login
    npx cap sync
import { SignInWithApple } from '@capacitor-community/apple-sign-in';
import { SocialLogin } from '@capgo/capacitor-social-login';

Key Change:新插件需要初始化步骤,之前没有这个需求。

// No initialization needed in old package
// For iOS: Basic configuration
await SocialLogin.initialize({
apple: {} // Basic iOS configuration
});
// For Android: Additional configuration required
await SocialLogin.initialize({
apple: {
clientId: 'YOUR_SERVICE_ID', // Service ID from Apple Developer Portal
redirectUrl: 'https://your-backend.com/callback' // Your backend callback URL
}
});

Important Note:对于iOS,提供基本配置,而Android需要额外的信息,包括Service ID和后端回调URL,用于Web OAuth认证。

Sign In

Sign In

登录过程从多个参数简化到更清晰的API:

const result = await SignInWithApple.authorize({
clientId: 'com.your.app',
redirectURI: 'https://your-app.com/callback',
scopes: 'email name',
state: '12345',
nonce: 'nonce'
});
const result = await SocialLogin.login({
provider: 'apple',
options: {
// Optional: Add scopes if needed
scopes: ['email', 'name'],
nonce: 'nonce'
}
});

The new plugin uses login() with provider: 'apple' 和可选权限范围而不是传递单独的配置值 clientIdredirectURI.

响应类型变更

响应类型变更

结果现在包括一个带有过期细节的对象和一个结构化的 accessToken 复制到剪贴板 profile 新功能

// Old response type
interface AppleSignInResponse {
response: {
user: string;
email: string | null;
givenName: string | null;
familyName: string | null;
identityToken: string | null;
authorizationCode: string | null;
};
}
// New response type
interface SocialLoginResponse {
provider: 'apple';
result: {
accessToken: {
token: string;
expiresIn?: number;
refreshToken?: string;
} | null;
idToken: string | null;
profile: {
user: string;
email: string | null;
givenName: string | null;
familyName: string | null;
};
};
}

更新的插件引入了前辈中不可用的功能:

检查登录状态

migrations

apple

// Not available in old package
const status = await SocialLogin.isLoggedIn({
provider: 'apple'
});

注销功能

// Not available in old package
await SocialLogin.logout({
provider: 'apple'
});

这些方法提供 isLoggedIn() 用于验证身份验证状态和 logout() 功能。

iOS 通过Xcode功能保持熟悉的设置程序:

  1. iOS 的设置基本保持不变。您仍然需要:
    • 在 Xcode 中启用 “使用 Apple 登录”功能
    • 在 Apple 开发者门户中配置您的应用
    • 无需为 iOS 进行任何额外的 code 变更

新插件提供了 Android 支持,但需要额外的设置: 在 Apple 开发者门户中创建一个 Services ID

配置一个 Web 认证端点

  1. 配置您的 Android 应用以处理 OAuth 流程
  2. iOS 的设置基本保持不变。您仍然需要:
  3. 在 Xcode 中启用 “使用 Apple 登录”功能
  4. 后端服务配置是必需的

请参阅以下Android设置指南以获取详细的Android设置指南 Android设置指南.

现代化的包提供:

  1. 统一的API 多个社交提供商(Google、Facebook、Apple)
  2. 改进的TypeScript类型 具有更好的类型定义
  3. 活跃的社区维护 与过时版本相比
  4. 内置 Android 支持 通过基于 Web 的身份验证
  5. 持久登录状态管理
  6. 更好的错误处理 一致的错误类型
  1. 现在需要显式初始化 - 没有默认配置
  2. 响应对象结构已更改 - 嵌套结果格式
  3. Android 实现需要一个后端服务 OAuth
  4. Token 刷新处理方式不同 - token 管理改进
  5. 错误处理和错误类型已发生变化 - 更详细的错误信息

详细的设置指南,请参阅 官方文档.

继续从 Apple Sign-In Migration 到 @capgo/social-login

标题:继续从 Apple Sign-In Migration 到 @capgo/social-login

如果您正在使用 Apple Sign-In Migration 到 @capgo/social-login 来规划身份验证和帐户流程,连接它 使用@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, 双因素认证 为实现细节使用双因素认证