跳过内容

Facebook 登录迁移到 @capgo/social-login

GitHub

本指南提供了从 @capacitor-community/facebook-login@capgo/capacitor-social-login. The new plugin modernizes Facebook authentication with a unified API that supports multiple social providers, improved TypeScript support, and enhanced capabilities.

  1. 移除旧包:

    终端窗口
    npm uninstall @capacitor-community/facebook-login
  2. 安装新包:

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

关键变更: 新的包需要在您的 code: 中进行明确的设置

// Old package required no explicit initialization in code
// Configuration was done only in native platforms
// New package requires explicit initialization
await SocialLogin.initialize({
facebook: {
appId: 'YOUR_FACEBOOK_APP_ID', // Required for web and Android
clientToken: 'YOUR_CLIENT_TOKEN' // Required for Android
}
});

登录方法现在接受一个提供者参数:

const FACEBOOK_PERMISSIONS = ['email', 'public_profile'];
const result = await FacebookLogin.login({ permissions: FACEBOOK_PERMISSIONS });
const result = await SocialLogin.login({
provider: 'facebook',
options: {
permissions: ['email', 'public_profile'],
limitedLogin: false,
nonce: 'optional_nonce'
}
});

响应类型变更

标题:响应类型变更

响应结构已现代化,使用更全面的配置文件对象:

// Old response type
interface FacebookLoginResponse {
accessToken: {
applicationId: string;
userId: string;
token: string;
expires: string;
};
recentlyGrantedPermissions: string[];
recentlyDeniedPermissions: string[];
}
// New response type
interface FacebookLoginResponse {
provider: 'facebook';
result: {
accessToken: {
token: string;
applicationId?: string;
expires?: string;
userId?: string;
permissions?: string[];
declinedPermissions?: string[];
} | null;
idToken: string | null;
profile: {
userID: string;
email: string | null;
friendIDs: string[];
birthday: string | null;
ageRange: { min?: number; max?: number } | null;
gender: string | null;
location: { id: string; name: string } | null;
hometown: { id: string; name: string } | null;
profileURL: string | null;
name: string | null;
imageURL: string | null;
};
};
}

关键差异:

  • Response now includes a provider field identifying the authentication provider
  • More detailed profile object with additional user information
  • Consistent structure across all social login providers
const result = await FacebookLogin.getCurrentAccessToken();
const isLoggedIn = result && result.accessToken;
const status = await SocialLogin.isLoggedIn({
provider: 'facebook'
});
const isLoggedIn = status.isLoggedIn;
await FacebookLogin.logout();
await SocialLogin.logout({
provider: 'facebook'
});

Platform Specific Changes

平台特定更改

安卓设置

安卓设置

现在通过initialize方法来处理配置:

// AndroidManifest.xml changes remain the same
// strings.xml become irrelevant
// Additionally initialize in your code:
await SocialLogin.initialize({
facebook: {
appId: 'your-app-id',
clientToken: 'your-client-token' // New requirement
}
});

重要: 安卓认证需要客户端令牌。

iOS设置

iOS设置
  1. iOS设置 AppDelegate.swift 保持不变:
import FBSDKCoreKit
// In application:didFinishLaunchingWithOptions:
FBSDKCoreKit.ApplicationDelegate.shared.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
// In application:openURL:options:
ApplicationDelegate.shared.application(
app,
open: url,
sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplication.OpenURLOptionsKey.annotation]
)
  1. 配置保持不变: Info.plist 复制到剪贴板
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb[APP_ID]</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>[APP_ID]</string>
<key>FacebookClientToken</key>
<string>[CLIENT_TOKEN]</string>
<key>FacebookDisplayName</key>
<string>[APP_NAME]</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbauth</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>

现在需要显式初始化

  1. - 在使用之前必须调用 响应对象结构已发生重大变化 initialize() - 新的嵌套结果格式,带有增强的配置数据
  2. 现在需要为 Android 使用客户端令牌 __CAPGO_KEEP_0__
  3. __CAPGO_KEEP_0__ - 需要额外的配置
  4. - 方法名和参数结构不同 - 基于提供商的方法
  5. - 错误处理和错误类型发生了变化 - 更详细的错误信息

新插件提供:

  • 统一的 API 在多个社交提供商(Google、Apple、Facebook)之间
  • 改进的 TypeScript 支持 带有更好的类型定义
  • 增强的用户资料 包含更多的用户信息
  • 活跃的维护 和社区支持
  • 一致的错误处理 在所有提供商中
  • 更好的令牌管理 带有适当的过期处理

为了更详细的设置说明,请参阅 官方文档.

从Facebook登录迁移继续到@capgo/social-login

标题:从Facebook登录迁移继续到@capgo/social-login

如果您正在使用 Facebook 登录迁移至 @capgo/social-login 来规划身份验证和帐户流程,连接它与 使用 @capgo/capacitor-social-login 为native能力在使用 @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, 和 两因素身份验证 关于两步验证的实现细节。